Node "Cannot use import statement outside a module" in CI - Fix It
This SyntaxError means Node parsed a file as CommonJS but the file uses ES import syntax. The runtime never reaches your logic; parsing fails first.
What this error means
A node command throws SyntaxError: Cannot use import statement outside a module at the first import line. The file uses import/export but Node is loading it as CommonJS.
node
import express from 'express';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)Common causes
No "type": "module" in package.json
A .js file with import syntax is parsed as CommonJS unless the nearest package.json declares type module.
Running uncompiled TypeScript or JSX directly
Node runs raw source that should have passed through a build or loader (tsc, tsx, babel) but did not.
How to fix it
Declare the package as ESM
- Add "type": "module" to package.json so .js files are treated as ES modules.
- Make sure your require() calls are converted to import or moved to .cjs files.
package.json
{
"type": "module"
}Transpile before running
- Run the build (tsc, esbuild, babel) so the emitted CommonJS no longer uses import syntax.
- Execute the compiled output instead of the source.
GitHub Actions
- run: npm run build
- run: node dist/server.jsHow to prevent it
- Pick one module system per package, set the matching "type" field, and run compiled output in CI rather than raw source that depends on a loader being present.
Related guides
Node "SyntaxError: Unexpected token 'export'" in CI - Fix the Parse ErrorFix the Node.js "SyntaxError: Unexpected token export" in CI by loading the file as ESM or compiling the ESM-…
Node ERR_REQUIRE_ESM "require() of ES Module" in CI - Fix the InteropFix the Node.js ERR_REQUIRE_ESM error in CI by switching to a dynamic import, pinning a CommonJS-compatible v…
Node CJS/ESM Default Import "is not a function" in CI - Fix the InteropFix a CommonJS-from-ESM default-import "is not a function" error in CI by reading the real callable off the i…