Node.js treats a file as an ES module when it has the .mjs extension, when the nearest package.json sets "type": "module", or when --input-type=module applies to STDIN input. Use import and export in those files; relative import specifiers must include the full file extension. filename and dirname do not exist in ES modules; use import.meta.filename and import.meta.dirname (or import.meta.url) instead. The .cjs extension marks CommonJS explicitly. Prefer ES modules for new code and keep CommonJS for interop with older packages; that preference is Koskamo editorial guidance. Scope: current Node.js LTS. Reviewed October 4, 2026.
ECMAScript modules in Node.js
Document v1 · published by Koskamo editorial · updated 2026-10-04
Source and provenance
- Original source
- nodejs.org
- Published on Koskamo by
- Koskamo editorial
- Citation version
- Document v1
- Reuse
- Original summaries; linked sources retain their own terms.