nuxt: Dynamic glob imports does not work with vite
Environment
- Operating System:
Linux - Node Version:
v14.16.0 - Nuxt Version:
3.0.0-27265876.3cd4494 - Package Manager:
npm@7.17.0 - Bundler:
Vite - User Config:
- - Runtime Modules:
- - Build Modules:
-
Describe the bug
Unpredictable dynamic imports like import(~/data/${name}json)` does not work with vite (dev-bundler)
Reproduction
https://stackblitz.com/edit/github-obezmd?file=app.vue
Additional context
As a workaround, one can use webpack using vite: false in nuxt.config.
Missing implementation is here but we probably need more to fix this.
Logs
Cannot read properties of undefined (reading 'stubModule')
at __instantiateModule__ (file://./.nuxt/dist/server/server.mjs:1771:11)
at __ssrLoadModule__ (file://./.nuxt/dist/server/server.mjs:1762:25)
at ssrImport (file://./.nuxt/dist/server/server.mjs:1787:13)
at ssrDynamicImport (file://./.nuxt/dist/server/server.mjs:1798:12)
at eval (file://./.nuxt/dist/server/server.mjs:1678:70)
at Module.withAsyncContext (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7295:21)
at setup (file://./.nuxt/dist/server/server.mjs:1678:47)
at _sfc_main.setup (file://./.nuxt/dist/server/server.mjs:1708:23)
at callWithErrorHandling (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6615:22)
at setupStatefulComponent (./node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:6241:29)
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 30 (11 by maintainers)
Is there is any update on this issue because i have the same problem
I would also be interested in a work around !
@danielroe Ok, took some time, but I found the issue.
It seems that the dynamic import is not working inside Nuxt components that aren’t top level components (it works in app.vue and pages). See the following reproduction : https://stackblitz.com/edit/github-l4ercq-4h6epp
Look for the following error in console :
TypeError: Failed to resolve module specifier '~/assets/images/home/img.png'Now, I try to use vite-plugin-dynamic-import to resolve this problem.
But in the end, I hope it can be integrated into vite.
2022-07-28
You may got the following error.
ERROR Unexpected token (1:0)
We have a PR that should further help here:
About the warning, I think it is too strict. But these are quite useful in most cases. I wonder if we should have a global option to disable them, or if we should not warn by default and have a new flag that would warn about “possible” issues that users could use to debug these problems.
@danielroe I’m trying to dynamically import images from the assets folder, using your stackblitz example.
But I get the following error :
TypeError: Failed to resolve module specifier '~/assets/images/img.png'Is it the right way of doing this ? What am I doing wrong ?
i Vite server using experimental vite-node...Just checked, it works with vite-node, with:
However, even it works in the runtime, the warning is still thrown by Vite. To bypass the warning, add
/* @vite-ignore */to your import statement:/cc @pi0 @patak-dev do you think we should be an option for vite to bypass this warning in this condition, or maybe this is just an anti-pattern we should ask users to be explicit about it?