← Back to Node.js, page 1

nodejs-0019 · Easy fresher

What are the main differences between CommonJS (require) and ES modules (import) in Node.js?

CommonJS loads files synchronously with require and module.exports. ES modules use import and export, are loaded asynchronously, are always in strict mode, support top-level await and have live bindings. Node decides the type from the file extension (.cjs, .mjs) or the nearest package.json type field.

Full answer

CommonJS (CJS) is the original Node module system:

  • require('x') runs the file at that moment and returns module.exports. It is synchronous and can be called anywhere, even inside an if.
  • It gives you __filename, __dirname, require, module and exports in each file.
  • What you get is whatever module.exports held when require returned. If the module later reassigns its own variable, importers do not see the change.

ES modules (ESM) are the standard JavaScript system:

  • import and export are static. They are analysed before the code runs, which allows tree shaking and early errors.
  • Loading is asynchronous, and top-level await is allowed.
  • Code is always in strict mode. There is no __dirname or require. Use import.meta.url, import.meta.dirname and createRequire.
  • Imports are live bindings. If the exporting module changes its exported variable, importers see the new value.
  • File extensions are required in relative imports ('./util.js').

How Node picks the type: .mjs is ESM, .cjs is CJS, and .js follows the "type" field in the nearest package.json ("module" or "commonjs", default commonjs).

Interop: ESM can import CJS (the default import is module.exports). CJS can load ESM with dynamic import(), and since Node 22.12 also with require() when the ESM graph has no top-level await.

javascript
// Save as app.mjs: the .mjs extension makes this file an ES module.
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { pathToFileURL } from 'node:url';

console.log('require defined:', typeof require);
console.log('__dirname defined:', typeof __dirname);
console.log('import.meta.url starts with file://:', import.meta.url.startsWith('file://'));

// Create a small CommonJS file and import it from this ES module.
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'esm-'));
const cjsFile = path.join(dir, 'grades.cjs');
fs.writeFileSync(cjsFile, "module.exports = { pass: (m) => m >= 40 };\n");

const grades = await import(pathToFileURL(cjsFile).href); // top-level await
console.log('default export is module.exports:', typeof grades.default.pass);
console.log('pass(39):', grades.default.pass(39), '| pass(40):', grades.default.pass(40));

fs.rmSync(dir, { recursive: true });

This file uses the .mjs extension. Top-level await and import.meta only work in ES modules. Save it as app.mjs and run it with node app.mjs. Output (Node.js v24.22.1, same on two runs): require defined: undefined __dirname defined: undefined import.meta.url starts with file://: true default export is module.exports: function pass(39): false | pass(40): true

Key points

  • CJS: require, synchronous, dynamic. ESM: import, static, asynchronous.
  • ESM has top-level await, strict mode and live bindings.
  • The type field and the extension decide the module type.
  • ESM has no __dirname or require.

Common mistakes

  • Calling require() in a .mjs file.
  • Leaving out the file extension in a relative ESM import.
  • Setting type module in package.json and forgetting to rename old .js CommonJS files to .cjs.
  • Believing import is just a different spelling of require.

Follow-ups

  • What is a live binding?
  • How do you import a JSON file in ESM?
  • What happens when a CJS file calls require on an ESM file?

Tips

  • Name the three things Node checks to pick the module type.
  • State that ESM is the standard and CJS is the legacy default.
  • Mention top-level await as a practical difference.