nuxt: [nuxt] [request error] [unhandled] [500] Cannot read properties of undefined (reading 'modules')
Environment
- Operating System:
Darwin - Node Version:
v16.18.0 - Nuxt Version:
3.1.1 - Nitro Version:
2.1.0 - Package Manager:
npm@8.19.2 - Builder:
vite - User Config:
nitro,runtimeConfig,vue,app,serverHandlers - Runtime Modules:
- - Build Modules:
-
Reproduction
I wish I could do this, but the project has internal dependencies that I cannot fully provide.
Describe the bug
After some time of things working just fine in our project, we have started to experience the following error. We have been struggling to troubleshoot this issue.
[Vue warn]: Unhandled error during execution of setup function
at <NuxtRoot>
[nuxt] [request error] [unhandled] [500] Cannot read properties of undefined (reading 'modules')
at _sfc_main.setup (./node_modules/nuxt/dist/app/components/nuxt-root.vue:76:16)
at callWithErrorHandling (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:157:22)
at setupStatefulComponent (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7176:29)
at setupComponent (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7131:11)
at renderComponentVNode (./node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:172:17)
at renderToString (./node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:450:26)
at Object.renderToString$1 [as renderToString] (./.nuxt/dev/index.mjs:970:24)
at Object.renderToString (./node_modules/vue-bundle-renderer/dist/runtime.mjs:173:40)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ./.nuxt/dev/index.mjs:1034:21
Additional context
I have read through other bug reports on this issue in the September timeframe and have verified that we don’t have things like multiple versions of Vue in the package-lock.json file. It is not a mono-repo etc. I don’t have a node_modules directory in my $HOME directory. The problem seems to only occur when running via ‘npm run dev’. The problem does not manifest itself after running an ‘npm build’ and running the app via the built output.
M-C02YNX67LVCG:src sdschetnan$ npm list --depth=10 | grep ’ vue@’ │ │ │ └── vue@3.2.45 deduped │ │ │ └── vue@3.2.45 deduped │ │ └── vue@3.2.45 deduped │ │ │ └── vue@3.2.45 deduped │ │ └── vue@3.2.45 deduped │ │ └── vue@3.2.45 deduped │ └─┬ vue@3.2.45 │ │ └── vue@3.2.45 deduped M-C02YNX67LVCG:src sdschetnan$
We experienced a similar issue yesterday with our base application where team members were reporting that it worked some of the time, but simply changing the name of the directory that contained the project could produce this error.
Logs
[Vue warn]: inject() can only be used inside setup() or functional components.
[Vue warn]: Server rendering context not provided. Make sure to only call useSSRContext() conditionally in the server build.
[Vue warn]: Unhandled error during execution of setup function
at <NuxtRoot>
[nuxt] [request error] [unhandled] [500] Cannot read properties of undefined (reading 'modules')
at _sfc_main.setup (./node_modules/nuxt/dist/app/components/nuxt-root.vue:76:16)
at callWithErrorHandling (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:157:22)
at setupStatefulComponent (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7176:29)
at setupComponent (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7131:11)
at renderComponentVNode (./node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:172:17)
at renderToString (./node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:450:26)
at Object.renderToString$1 [as renderToString] (./.nuxt/dev/index.mjs:970:24)
at Object.renderToString (./node_modules/vue-bundle-renderer/dist/runtime.mjs:173:40)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ./.nuxt/dev/index.mjs:1034:21
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 6
- Comments: 17 (7 by maintainers)
Commits related to this issue
- build(nuxt): downgraded to nuxt 3.0.0 due to build errors on 3.1.1 — committed to stephenjason89/NuxtVGP by stephenjason89 a year ago
Encountered the same.
The error comes from
nuxt-root.vue, so I looked up the transformed code withVITE_NODE_DEBUG_DUMP=true pnpm devThe snippet of the error code in
nuxt3/dist/app/components/nuxt-root.vue:76:16is:It seems the server did not run in Vue SSR context, where
useSSRContext()returnsundefined. This also matches the previous errors in the console:In that sense, disabling SSR would bypass this error:
Haven’t dive deep into this, but hopefully, this could provide a bit more context
I am reviewing https://github.com/vuejs/core/pull/7590 again, and noticed that the first repro provided by @harlan-zw here is now working properly with Nuxt 3.2+.
Fundamentally, I still believe the “root cause” is that there are duplicated copies of Vue, not that Vue should be made to allow multiple copies working together. The fix in https://github.com/vuejs/core/pull/7590 is more like a band-aid that happens to make some cases work, but isn’t a technically complete fix either. I am trying some alternative implementation along the same veins, but I would like to get an updated minimal repro with latest Nuxt to make sure we are making the right trade-offs.
I made an upstream fix to Vue.js (https://github.com/vuejs/core/pull/7590) that ultimately fixes the root causes of this issue and the ones alike in the future. And confirming locally it does fix some reproductions tried.
I hope we can find a faster fix and deduplicate Vue to using the same instance of Vue in the meantime until that (hopefully) gets landed upstream.
Reproduction: https://stackblitz.com/edit/nuxt-starter-ykdhnn?file=nuxt.config.ts (hacky, see notes).
I started running into this issue quite a bit, I believe it’s related to Nitro v2 and did some digging today.
For me, what I’ve found is that this occurs when your workspace directory is being transpiled.
I have a replication with my nuxt-og-image repo: https://github.com/harlan-zw/nuxt-og-image/tree/temp/bugged-playground (
pnpm i && pnpm run dev)My setup is a
playgrounddirectory as a subfolder of a module. The playground links the module using a link in package.jsonWith my setup it seems like a deeper issue with the node module resolution, pointing to the directory with the package.json instead of the
/distfolder, leading it to inline vue again?The issue was pretty easy to solve in this case, by swapping to a directory path instead of relying on the node module resolving. Don’t think it helps OP in this case.
I tried to replication this in StackBlitz without much success using a similar setup as the above. I had to just manually point to the workspace dir to be transpiled and I could replicate it, see reproduction.
So in summary, I believe this is a Nitro issue with the inlining of deps, possibly module resolution issue (possibly with symlinks?). Related code would be this function https://github.com/unjs/nitro/blob/main/src/rollup/plugins/externals.ts#LL22C19-L22C19
@pi0 might have some ideas
Hi! Just started and still investigating this issue too since happened with latest
content-windand 3.1.It seems the root cause is that we are using different “builds” of vue (
@vue/runtime-core, also server-renderer) which each use their own global vue context instance variable forgetCurrentInstance().(normally
@vue/runtime-core.cjs.jsonly is required and once)@harlan-zw I don’t think it is related to Nitro 2 externals. External plugin will be used for production build only in dev it is direct esm import of Nuxt bundle. Using resolved path might be affecting Node Modules search path and preventing duplicate resolutions as hotfix.
@antfu Thanks for debugging tip! I have tried to add an inline check guard to
@vitejs/plugin-vue/dist/index.mjs(where the code comes from). You are right the issue isnullcontext because even bypassing that, another composable (useNuxtInstance) fails on server side startup.A little more on this. It seems that nuxt doesn’t like any directory prefixed with src. For example, I changed my directory name from ‘src’ to ‘code’ and the project works fine. If I change the directory to ‘src-code’ it fails with error above. If I change to something like ‘my-code’ or ‘the-code’ the project runs without error.
First of all, thank you to the others who have added feedback and clues to this report - much appreciated!
I whittled down my project down to almost nothing. The project is attached below as tst.zip. It seems that the issue has little to do with my project in general, as I have it down to a single dependency and basically - hello world. My project root has some dockerfiles and other stuff that is for deployment to our OpenShift environment. The nuxt part of the app is in the ‘src’ directory. Strangely enough, one thing I have noticed on my end is if I change my ‘src’ directory name to something else like - ‘code’, wipe out node_modules, run npm install, and re-run the app via ‘npm run dev’ the app will work without receiving the errors. If I then change my directory back to ‘src’ again and do the same, the app will once again fail with the errors reported above. I have tried this several times with the same result.
tst.zip