Skip to content

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.

Why it happens

Node.js supports two module systems. CommonJS uses require() and module.exports; ES modules (ESM) use import and export. A .js file is CommonJS unless the closest package.json says "type": "module". When Node loads a CommonJS file that contains import, it stops with this SyntaxError.

Fixes

SituationFix
Your own Node project, all ESMAdd "type": "module" to package.json
Only one file should be ESMRename it to .mjs
You want to stay on CommonJSReplace 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"
JestTransform ESM (babel-jest or ts-jest) or run Jest in ESM mode
Browser script tagAdd type="module" to the <script> tag
package.jsonjson
{
  "name": "my-service",
  "type": "module",
  "main": "src/server.js"
}
tsconfig.jsonjson
{
  "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.

Frequently asked questions

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.

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.