MJS CJS
File conversion

Convert MJS to CJS

QUICK ANSWER
Is it possible to convert MJS to CJS?
Yes - MJS converts to CJS.

A .mjs file uses ES module syntax, while .cjs uses CommonJS. A bundler like esbuild or Babel rewrites import to require and export to module.exports automatically. The main blocker is top-level await, which CommonJS cannot represent.

On this page

Tested on macOS, Windows & Linux
Last verified Sep 2026

More free converters

HEIC, PNG, WEBP, MP3 and more - every tool here is free.

Open the toolbox

Why convert MJS to CJS?

People convert to .CJS to run code in older Node.js setups or tools that expect CommonJS, or to ship a require-compatible build alongside an ESM one. The logic stays the same; only the module wiring changes.

How to convert MJS to CJS

Babel OPEN-SOURCE

Add @babel/plugin-transform-modules-commonjs to your config, then run babel input.mjs -o output.cjs to rewrite imports to require.

Rollup FREE

Set output.format to cjs in rollup.config.js and build; Rollup emits a CommonJS bundle from your ESM entry.

About these formats

Quality & what to watch

  • Top-level await has no CommonJS equivalent and will fail to convert.
  • Named exports become properties on module.exports, and interop with default exports can differ subtly.
  • Relative import paths and import.meta.url may need adjusting for the CommonJS output.

Frequently asked questions

Can I convert by hand?
For small files yes: swap import x from 'y' for const x = require('y') and export for module.exports.
Why did my build fail?
Most often top-level await, which CommonJS cannot express; move it inside an async function.
Do I need to rename to .cjs?
Yes, if your package.json has "type": "module"; the .cjs extension forces Node to treat it as CommonJS.
Which tool is fastest?
esbuild is the quickest for a single command; Babel and Rollup give finer control over the output.