0PricingLogin
Node.js Backend Development Bootcamp · Lesson

Migrating from CommonJS to Native ES Modules

Convert require/module.exports to import/export and handle dual-package and interop pitfalls.

Two Module Systems, One Runtime

Node.js historically used CommonJS (CJS): you load code with require() and expose it with module.exports. Modern JavaScript has a built-in standard, ES Modules (ESM), using import and export.

  • CommonJS loads modules synchronously at runtime.
  • ESM is statically analyzed, loaded asynchronously, and is the official ECMAScript standard.

Node now supports both, but mixing them has rules. This lesson walks you through migrating a backend project from CJS to native ESM cleanly.

// CommonJS (old)
const fs = require('fs');
module.exports = { readConfig };

// ES Modules (new)
import fs from 'fs';
export { readConfig };

Telling Node You Mean ESM

Node decides how to treat a file based on its extension and the nearest package.json:

  • "type": "module" in package.json.js files are treated as ESM.
  • No type field (or "commonjs") → .js files are CommonJS.
  • .mjs is always ESM; .cjs is always CommonJS, regardless of type.

The cleanest migration step is adding "type": "module" once, then fixing the files it breaks.

{
  "name": "my-api",
  "version": "1.0.0",
  "type": "module",
  "main": "src/server.js",
  "scripts": {
    "start": "node src/server.js"
  }
}

All lessons in this course

  1. Migrating from CommonJS to Native ES Modules
  2. Configuring tsconfig for Node Backend Projects
  3. Type-Safe Environment Config and Runtime Validation
  4. Fast Iteration with tsx, Hot Reload, and Source Maps
← Back to Node.js Backend Development Bootcamp