Why convert MJS to JS?
People convert .mjs to plain .js to drop the explicit ES-module extension and let the project's package.json decide the module system, or to ship CommonJS output that older Node versions and tools can load without ESM.
How to convert MJS to JS
Rename and set package.json type EASIEST
Rename the file to .js and add "type": "module" to package.json so Node keeps treating it as an ES module.
Babel OPEN-SOURCE
Install @babel/plugin-transform-modules-commonjs (or @babel/preset-env) and run babel file.mjs --out-file file.js to rewrite import/export into CommonJS require/module.exports.
esbuild OPEN-SOURCE
Run esbuild file.mjs --format=cjs --platform=node --outfile=file.js to emit a CommonJS .js bundle from the ES module.
Visual Studio Code FREE
Open the .mjs in VS Code, rename it to .js, and use the editor to convert import/export statements by hand for small files.
About these formats
An .mjs file is JavaScript source code that Node.js treats as an ECMAScript module, meaning it uses import and export instead of CommonJS require. The extension exists so Node can tell ES…
Open .MJS details →A JS file is a JavaScript source code file - plain, readable text that powers web page interactivity and server-side apps via Node.js. Open it in any code editor such as VS Code or…
Open .JS details →Quality & what to watch
- Renaming alone does not change the code; if
package.jsonis not set to"type": "module", ESM syntax in a.jsfile will throw aSyntaxError. - Transpiling to CommonJS changes
import/exportintorequire/module.exportsand can alter top-levelawaitandimport.metabehavior. - A bundler like esbuild may inline dependencies and strip comments, so the output
.jsis not a line-for-line copy of the source.
Frequently asked questions
Is `.mjs` the same as `.js`?
.mjs always means an ES module, while .js follows whatever package.json sets - CommonJS by default, or ESM if "type": "module" is present.Can I just rename `.mjs` to `.js`?
package.json has "type": "module". Otherwise Node treats the .js as CommonJS and ESM syntax will error.How do I keep ESM after renaming?
"type": "module" to your package.json, or use the .mjs extension. Both tell Node to parse the file as an ES module.