How Node treats .js and .mjs files
Use file extensions and the package type to choose between CommonJS and ES modules in Node.
In Node, .mjs means ES module and .cjs means CommonJS. A .js file can use either system, depending on the nearest parent package.json. Set its type field to make the choice explicit.
Choose the package type
"type": "module"makes.jsfiles in the package use ES modules, withimportandexport."type": "commonjs"makes them use CommonJS, withrequire()andmodule.exports.
If you need one file to use a different system, give it the explicit extension. In a CommonJS package, for example, save an ES module script as tools.mjs and run node tools.mjs.
Browser modules
For scripts loaded in a browser, <script type="module" src="./app.js"></script> selects module mode. You can use either extension as long as your server sends a JavaScript media type such as text/javascript.
Node supported ES modules without the experimental flag before this post was published. See the Node module history for the release timeline.
diagram
Two module systems, one decade
Update, November 2026. Node enabled syntax detection by default in 22.7.0. An ambiguous .js file with ES module syntax can now run as a module even without an explicit type.