Error explained
SyntaxError: Cannot use import statement outside a module
Node.js found an import statement in a file it is treating as CommonJS. Tell Node the file is an ES module, or compile it to CommonJS.
3 min read8 sectionsUpdated
On this page
Key points
- Node decides module type per file: .mjs is ESM, .cjs is CommonJS, .js follows the nearest package.json "type".
- The quickest fix is "type": "module" in package.json, if the whole package is ESM.
- Test runners and TypeScript have their own settings that must agree.
- Mixing require and import in one file does not work.
| Situation | Fix |
|---|---|
| Your own Node project, all ESM | Add "type": "module" to package.json |
| Only one file should be ESM | Rename it to .mjs |
| You want to stay on CommonJS | Replace import with require(), or use dynamic import() |
| TypeScript compiled to CommonJS but run as ESM (or the reverse) | Align "module" in tsconfig with package.json "type" |
| Jest | Transform ESM (babel-jest or ts-jest) or run Jest in ESM mode |
| Browser script tag | Add type="module" to the <script> tag |
{
"name": "my-service",
"type": "module",
"main": "src/server.js"
}{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2022"
}
}When the error comes from nodemodules
A dependency that ships only ESM cannot be require()d from older CommonJS code (recent Node versions can require some ESM synchronously, with limits). Options: switch your project to ESM, use await import('pkg') where you need it, or pin an older CommonJS version of the dependency. In Jest, add the package to transformIgnorePatterns exceptions so it gets transformed.
Catching it in production
This error usually appears at startup, so a crashed deploy is the symptom: health checks fail and pods restart. Uptime checks and restart-count alerts catch it within minutes, and the startup log shows the exact file.
VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.
Can I use import and require in the same file?
Not as static statements. In ESM you can create a require function with createRequire from node:module; in CommonJS you can use dynamic import().
Does "type": "module" break my existing require calls?
Yes, in .js files of that package. Rename those files to .cjs or convert them to import.
Related reading
- ModuleNotFoundError in Python: how to fix it: Fix Python ModuleNotFoundError: wrong virtual environments, pip installing to another interpreter, sys.path issues and packaging mistakes in Docker builds.
- Error: socket hang up: Why Node.js reports "socket hang up", from servers closing connections and timeouts to proxies and TLS problems, with steps to find the cause.
- 410 Gone: what it means and when to use it: 410 Gone explained: how it differs from 404, how search engines treat it, when to remove pages with 410, and how to fix a 410 you did not expect.
- EADDRINUSE: address already in use and how to fix it: Fix EADDRINUSE in Node.js and other servers: find which process holds the port, handle old processes after a crash and avoid race conditions in restarts.
- ENOTFOUND getaddrinfo: how to fix DNS errors in Node.js: Fix getaddrinfo ENOTFOUND and EAI_AGAIN in Node.js: wrong hostnames, a URL passed as host, container DNS and thread pool exhaustion, with test commands.
- Error: connect ETIMEDOUT: What ETIMEDOUT means, how it differs from ECONNREFUSED, the usual causes such as firewalls, security groups and wrong IPs, and how to set timeouts.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.