Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Armani's avatar
Level 17

Vite won't let JavaScript to work properly

I created a Inertia/Vue project and I have a separated JavaScript file that linked to layout using @vite helper. there's simple JavaScript code that gives me error:

document.querySelector(".navbar-menu").innerHTML;

the error:

Uncaught TypeError: document.querySelector(...) is null

I know it is because Vite but I don't know how to solve it.

0 likes
24 replies
Sinnbeck's avatar

We need some more information. Can you show some more? Are you sure it is run after the dom is loaded, and that you have an element with the class "navbar-menu"

Is the @vite helper at the end of the body tag?

Armani's avatar
Level 17

@Sinnbeck @vite is in the head

@vite(['resources/js/template.js',])

and inside template.js I have this:

(function () {
	("use strict");
	var navbarMenuHTML = document.querySelector(".navbar-menu").innerHTML;
})();

And I have element with class nav-menu

Sinnbeck's avatar

@Armani Arent you calling it?

(function () {
	("use strict");
	var navbarMenuHTML = document.querySelector(".navbar-menu").innerHTML;
})() //call it?

If you call document.querySelector(".navbar-menu").innerHTML; in the browser console, it works ?

Armani's avatar
Level 17

@Sinnbeck No difference. I can't work with Vuejs because of this problem.

Sinnbeck's avatar

@Armani How about

(function () {
	("use strict");
    setTimeout(() => {
        var navbarMenuHTML = document.querySelector(".navbar-menu").innerHTML;
    }, 0)
	
})()
Armani's avatar
Level 17

@Sinnbeck Yes it works like that and I tried before, I want to know why it needs delay.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@Armani And the class is coming from a blade file and not vue right? I am struggling to recreate the issue.

Be aware that if its added in vue, it is probably because your helper file is loaded first

Sinnbeck's avatar

@Armani Ok. Then it is most likely due to vue is loaded after your helper.js file is.

Sinnbeck's avatar

@Armani Hard to say when I dont know your code. I cannot think of any reason you would need to use a query selector when using inertia/vue. Why not just have the logic directly in vue?

Armani's avatar
Level 17

@Sinnbeck This file is needed for template functionality and some JavaScript plugins, it's a Bootstrap Template

Sinnbeck's avatar

@Armani Then I dont see any way of doing it without the setTimeout. Maybe vue has a hook for when the page is full rendered you can use and load the file there?

1 like
Sinnbeck's avatar

@Armani I got this working in react. I assume it works in vue as well

  setup({ el, App, props, plugin }) {
    createApp({ render: () => h(App, props) })
      .use(plugin)
      .mount(el)
    import('./helper.js') //here
  },
Armani's avatar
Level 17

@Sinnbeck It gives me this error in console:

unreachable code after return statement
Sinnbeck's avatar

@Armani Oh there is a return? Can you show yours?

This is my test

setup({el, App, props}) {
    render(<App {...props} />, el)
    import('./test.js')
},
Armani's avatar
Level 17

@Sinnbeck Here it is:

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
    setup({ el, App, props, plugin }) {
        return createApp({ render: () => h(App, props) })
            .use(plugin)
            .use(ZiggyVue, Ziggy)
            .mount(el);
            import ('./template.js');
    },
    progress: {
        color: '#4B5563',
    },
});
Sinnbeck's avatar

@Armani Are you sure you need to return anything? That isnt in the docs

createInertiaApp({
    title: (title) => `${title} - ${appName}`,
    resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
    setup({ el, App, props, plugin }) {
        createApp({ render: () => h(App, props) }) //removed return
            .use(plugin)
            .use(ZiggyVue, Ziggy)
            .mount(el);
          import ('./template.js');
    },
    progress: {
        color: '#4B5563',
    },
});
Armani's avatar
Level 17

@Sinnbeck I didn't touch that part, when I installed Inertia it did all of it.

Please or to participate in this conversation.