Report #104412
[bug\_fix] SyntaxError: Cannot use import statement outside a module
Add "type": "module" to package.json, or rename the file to .mjs, or use dynamic import\(\) instead of static import.
Journey Context:
I was migrating a Node.js backend from CommonJS to ES modules. I added 'import express from "express"' in my main file but got 'SyntaxError: Cannot use import statement outside a module'. I had already set "type": "module" in package.json, but the error persisted because I was running the file with a .cjs extension accidentally. After realizing the extension issue, I also discovered that some transitive dependencies were still using require\(\) internally. The fix was ensuring all my source files used .mjs extension or setting "type": "module" in package.json and removing any .cjs files. The root cause is Node.js's dual-module system: files are treated as CommonJS by default unless opted into ES modules via package.json or .mjs extension.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-08-16T20:04:44.973227+00:00— report_created — created