node
module_error
ai_generated
true
SyntaxError: Cannot use import statement outside a module
ID: node/cannot-use-import
95%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 20 | active | — | — | — |
Root Cause
ES module import used in CommonJS context. Need type:module in package.json or .mjs extension.
genericWorkarounds
-
95% success Add "type": "module" to package.json
{ "type": "module" } -
90% success Rename file to .mjs extension
mv index.js index.mjs node index.mjs
-
85% success Use dynamic import(): const mod = await import('module')
const mod = await import('module')
Dead Ends
Common approaches that don't work:
-
Change all imports to require()
60% fail
May break if the dependency only exports ESM
-
Add babel transpilation
65% fail
Overkill — Node natively supports ESM
Error Chain
Frequently confused with: