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"inpackage.json→.jsfiles are treated as ESM.- No
typefield (or"commonjs") →.jsfiles are CommonJS. .mjsis always ESM;.cjsis always CommonJS, regardless oftype.
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
- Migrating from CommonJS to Native ES Modules
- Configuring tsconfig for Node Backend Projects
- Type-Safe Environment Config and Runtime Validation
- Fast Iteration with tsx, Hot Reload, and Source Maps