Cannot use import statement outside a module
See original GitHub issueThis is a followup on #2161. This issue had been fixed, but it was reintroduced in the latest sveltekit.
As asked, here is a reproduction: https://github.com/lovasoa/SvelteKit-2161
The stacktrace is
Cannot use import statement outside a module
/tmp/SvelteKit-2161/node_modules/@popperjs/core/dist/esm/popper.js:1
import { popperGenerator, detectOverflow } from "./createPopper.js";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1032:15)
at Module._compile (node:internal/modules/cjs/loader:1067:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:168:29)
at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
at async Promise.all (index 0)
at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
@lovasoa can you file a new issue with a reproduction? I don’t have a way to reproduce and investigate at the moment
_Originally posted by @benmccann in https://github.com/sveltejs/kit/issues/2161#issuecomment-1085851247_
Issue Analytics
- State:
- Created a year ago
- Comments:12 (9 by maintainers)
Top Results From Across the Web
"Uncaught SyntaxError: Cannot use import statement outside ...
This means that you're using the native source code in an unaltered/unbundled state, leading to the following error: Uncaught SyntaxError: ...
Read more >Cannot use import statement outside a module [React ...
When building a web application, you may encounter the SyntaxError: Cannot use import statement outside a module error.
Read more >How to fix "cannot use import statement outside a module"
I stumbled on this error: Uncaught SyntaxError: cannot use import statement outside a module while importing a function from a JavaScript ...
Read more >How to solve: cannot use import statement outside a module
When you see the error message Uncaught SyntaxError: cannot use import statement outside a module, it means you're using an import statement ......
Read more >SyntaxError: Cannot use import statement outside a module
If you are using node application and want to use the import statement, then you will also get the " Cannot use import...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is a bug in
@popperjs/core
— Node is behaving as it should. Popper should either.mjs
extension for all the modules indist/esm/**/*.js
, ordist/esm/package.json
with{"type":"module"}
, or{"type":"module"}
in the rootpackage.json
It should also declare its exports using the
"exports"
field in package.json.As far as I can tell, the package has been superseded by floating-ui, which appears to do things differently, so maybe this is fixed already.
Either way, it can be worked around by populating Vite’s
ssr.noExternal
config:Dropping the import of the two enums solved the problem but I had to redefine them locally.