designwithfocus

JavaScript & front end

ES Modules vs CommonJS for Sharing JavaScript Files

By Design With Focus Team 6 min read
Two JavaScript files, one using import and one using require

Short answer

Use ES modules for new JavaScript you share with browsers, and mark Node packages with an explicit type. CommonJS, require and module.exports, is Node's original module system. The Node.js docs read for this guide, v26.14.0, say ambiguous .js files are tried as CommonJS first. Set "type" to "module" or "commonjs" so tools are not guessing. Browsers load ES modules with a script type of module.

Last verified against the sources listed below.

Key takeaways

  • Node.js has two module systems: CommonJS, the original, and ECMAScript modules.
  • import and export mark an ES module. require and module.exports mark CommonJS.
  • Use .mjs or "type": "module" for ES modules. Use .cjs or "type": "commonjs" for CommonJS.
  • If a .js file has no marker, Node tries CommonJS first, then ES module syntax if that parse fails.
  • ES modules have no require, __dirname, or __filename. import.meta.dirname replaces __dirname.
  • In HTML, type="module" selects a module script. The HTML spec says those fetches use CORS when cross-origin.

ES modules vs CommonJS is a choice about how one JavaScript file gets values from another. For new code, prefer ES modules and say so in the file or in package.json. CommonJS remains the right loader when the package already publishes require and module.exports.

Node’s ESM documentation, read here as v26.14.0, says Node.js fully supports ECMAScript modules and still interoperates with CommonJS, which it calls the original module format. The same page says there are two module systems, not a slow replacement of one by the other.

How does each system share a file?

An ES module names its public values with export and pulls them in with import. Node’s example is export { addTwo } in addTwo.mjs, then import { addTwo } from './addTwo.mjs' in another file. The extension is part of the relative specifier.

CommonJS assigns what it shares. The CommonJS docs show const circle = require('./circle.js') and say you can assign a new value to module.exports, such as a function, an object, or a class. Assigning to exports alone does not replace the module. You have to assign module.exports.

MDN’s import page says those import bindings are live and read-only. The exporter can update them. The importer cannot point the name at something else. MDN marks the static import as widely available since May 2018, and it says a module is in strict mode.

When should you choose ES modules?

Choose them when the file should load in a browser or when you are starting a Node package and can set the type. The HTML script element says a type value of module means the script is a JavaScript module script, tied to the Module production in the language spec. A classic script uses the Script production instead. The same page says module scripts require CORS for cross-origin fetching.

In Node, mark the file with .mjs, with "type": "module" in the nearest package.json, or with --input-type=module. The packages docs say authors should set type even for an all-CommonJS package, so the choice is explicit.

The language side is ECMAScript. ECMA-262, the ECMAScript 2027 specification, puts scripts and modules in section 16, with modules under 16.2. Node and the browser are hosts, and they decide how a specifier becomes a file. The syntax of import and export is the language.

When should you choose CommonJS?

Choose CommonJS when the code you depend on is already require and module.exports, or when you are maintaining a package that has not moved. Node’s CommonJS page calls that system the original way to package JavaScript for Node.js.

Mark those files with .cjs, with "type": "commonjs", or with --input-type=commonjs. If a .js file has no marker, the packages docs say Node tries to run it as CommonJS first. If that fails because the parser found ES module syntax, Node evaluates it again as an ES module. Writing ESM syntax in that ambiguous state has a performance cost, which is one reason the docs want an explicit type.

require() always uses the CommonJS loader. import() always uses the ES module loader. A dynamic import() is legal inside CommonJS, so you can pull an ES module into an older file without renaming the whole package on day one.

How do the two formats compare?

The practical differences are the ones Node lists, not a benchmark this guide did not read.

Question ES modules CommonJS
Share a value export, then import module.exports, then require
File marker .mjs, or type module .cjs, or type commonjs
Unmarked .js Used if the syntax must be ESM Tried first
Directory of the file import.meta.dirname __dirname
This file’s path import.meta.filename __filename
Is this the entry? import.meta.main require.main === module
Relative path Extension required Extension often optional
Browser script type=module Not a browser module format

Node also says ES modules have no require, exports, module.exports, require.cache, or require.extensions. Addons are not loaded by an ES module import. The docs say to use module.createRequire() or process.dlopen for that case.

One performance note is in the ESM docs, and it is narrow. When you import a built-in, all named exports are populated even if you do not use each one, which can make that initial import slightly slower than require() or process.getBuiltinModule(). That sentence is about built-ins, not a claim that every ES module app is slower.

What breaks when you mix them carelessly?

The usual break is a .js file that uses import while the package has no "type": "module". Node may still accept it after the CommonJS parse fails, and the docs call that path a cost. Set the field.

The next break is a missing extension. import x from "./utils" is not the CommonJS lookup that finds utils/index.js. Node’s ESM rules want the file name. MDN says browsers do not probe a list of candidates either.

JSON is a third small trap. Node’s ESM page says JSON modules are no longer experimental as of v23.1.0, v22.12.0, v20.18.3, and v18.20.5, and that with { type: 'json' } is mandatory. The import exposes only a default export.

A generated component is just another module. The SVG to JSX tool emits a component you can import from an ES module file.

How that file runs in the browser is JavaScript.

How a Node server loads it is back-end development.

Frequently asked questions

Can one Node project use both module systems?

Yes, with explicit files and a dynamic import. Node's ESM docs say import() works in both CommonJS and ES modules, and can load either kind. A CommonJS require can load synchronous ES modules, meaning modules that do not use top-level await. You cannot drop require, exports, or module.exports into an ES module file and expect them to exist. The docs say those names are absent there. If you need a require inside an ES module, the docs say to build one with module.createRequire().

Why does a relative import need a file extension?

Node's ESM docs say the file extension is always necessary for relative specifiers such as ./startup.js. MDN explains the browser reason: if the specifier could mean several URLs, the browser would have to probe utils, utils.js, and utils/index.js. The HTML spec resolves a module specifier as a URL. CommonJS allowed require("./utils") to find an index file. That shorthand is the CommonJS habit. It is not how ESM relative imports work in Node or in the browser.

What replaces __dirname in an ES module?

Node's difference list says __filename and __dirname are not available in ES modules. The replacements it names are import.meta.filename and import.meta.dirname. require.main === module becomes import.meta.main. require.cache is not used by import, because the ES module loader has its own cache. Those are API renames, not a reason to avoid ES modules. They are the names you look up when you are moving one file across.

What does package.json type actually change?

It tells Node how to read .js files in that package. The packages docs say a nearest parent package.json with "type": "module" makes those .js files ES modules. "type": "commonjs" keeps them CommonJS. .mjs is always an ES module marker. .cjs is always CommonJS. The docs encourage package authors to set type even when every file is CommonJS, so build tools are not guessing and so a future default change does not surprise the package.

Are import bindings live?

MDN says a static import creates read-only live bindings. The exporting module can reassign the binding, and importers see the new value. The importing module cannot reassign the name. Import declarations sit at the top level of a module, and the file must be parsed as a module. In HTML that means type="module" on the script element. MDN also says modules are automatically in strict mode. A classic script without that type is not a module, and an import declaration there is a syntax error.

Sources