Unexpected token 'export' while importing modules in SSR in dev mode
Environment
- Operating System:
Windows_NT - Node Version:
v19.1.0// I tested also node v16 - Nuxt Version:
3.0.0 - Nitro Version:
1.0.0 - Package Manager:
[email protected] - Builder:
vite - User Config:
server,loadingIndicator,css,app,modules,i18n,vite,nitro - Runtime Modules:
@nuxtjs/[email protected] - Build Modules:
-
Reproduction
I am trying to repro in a smaller project (in stackblitz), I failed to repro the problem. maybe you can help me to debug the scene and find what going on.
Describe the bug
In SPA mode (dev only), all is working good and fast (better then v2).
In SSR mode, only in dev I am reciving this error from the server:
500
Unexpected token 'export'
at internalCompileFunction (node:internal/vm:74:18)
at wrapSafe (node:internal/modules/cjs/loader:1128:20)
....
So I was connected a debugger, both DevTools one and VS2022 one, and start looking the function internalCompileFunction to see who failed to compile.
I found a code imported via import ifetch from '~/common/fetch.js'.
so I was removed the "export" / "import" from this code, then see the same error on other file.
after I was commented some files, I was see the same problem in the I18N module - it can't load its JSON files becase the module loader failed.
I tried to change the package.json configuration to type: module.
This solve this problem, but then I see an other problem
Cannot find package '~' imported from e:\inetpub\inn\nuxt3b\store\news.js
The original line:
import { nIDate, SetDate } from '~/common/UserRelated/Dates'
so I changed this code to
import { nIDate, SetDate } from '../common/UserRelated/Dates'
no change.
I added a space to nuxt.config.ts, and now It working.
So we have two problems.
-
why the server is loading the files as a commonjs ? (I tried to repro tje problem in stackblitz but with no success)
-
Why the "~" isn't working in module mode?
Thanks for all! I like the framework and use it for large website and happy.
Additional context
no special nitro plugins
no special middlewares
no special vite config, except vite: { css: { preprocessorOptions
(in this ^ code, the ~/ is working).
Logs
i Vite client warmed up in 6702ms 20:51:27
√ Nitro built in 473 ms nitro 20:51:28
(node:28076) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
[nuxt] [request error] [unhandled] [500] Unexpected token 'export' 20:51:30
at internalCompileFunction (node:internal/vm:74:18)
at wrapSafe (node:internal/modules/cjs/loader:1128:20)
....