CommonJS vs ES Modules in Node.js
Master the two module systems that power the Node.js ecosystem and write more organized, reusable code.
/* Ready to dive into modules? */
The Classic: CommonJS
Node.js traditionally uses CommonJS. You import modules with the require()
function and export them by assigning to module.exports
. This system is synchronous, meaning modules are loaded one by one, blocking further execution until complete.
The Standard: ES Modules
ES Modules are the modern standard for JavaScript. They use the import
and export
keywords. This system is asynchronous and allows for static analysis, which enables optimizations like "tree-shaking" (removing unused code).
Key Differences
The key differences are syntax (require
vs import
), loading mechanism (synchronous vs asynchronous), and how they handle the this
keyword at the top level. ES Modules are generally preferred for modern projects.
Compatibility in Node.js
You can use ES Modules in Node.js by naming files with .mjs
or setting "type": "module"
in your package.json
. While you can import CommonJS modules into ES Modules, the reverse is more complex. Understanding this interoperability is key for mixed codebases.
Practice Zone
Interactive Test 1: Syntax Sort
Drag the keywords to their correct module system.
Arrastra en el orden correspondiente.
Arrastra las opciones:
Completa el código:
Interactive Test 2: Complete the Code
Rellena los huecos en cada casilla.
// CommonJS const myModule = ('./myModule'); module. = someValue; // ES Modules myFunction from './myFunction.js'; const myVariable = 10;
Practice Example: Code Editor
Create two files. In one, export a function using CommonJS. In the other, import and call that function.
Modules in the Real World
Understanding the difference is one thing, but using them effectively in a project is another. Here are some practical tips.
1. Interoperability: Using Both Together
Modern Node.js allows you to import
a CommonJS module directly into an ES Module. This is crucial for using the vast ecosystem of older NPM packages that are still CJS-based.
// my-app.mjs (ESM)
import express from 'express'; // express is CJS
const app = express();
app.listen(3000);
2. Activating ES Modules
To tell Node.js your project uses ES Modules, add "type": "module"
to your package.json
. This makes .js
files default to ESM, and you can rename CJS files to .cjs
if needed.
// package.json
{
"name": "my-project",
"type": "module"
}
Practical Takeaway: Start new projects with ES Modules ("type": "module"
) for future-proofing, but be prepared to work with CommonJS packages, as the ecosystem is still in transition.
Node.js Modules Glossary
- require()
- The CommonJS function used to import a module. It reads and executes the module file synchronously.
- module.exports
- In CommonJS, this object is what's returned from a
require()
call. You attach your exports to it. - import
- The ES Modules keyword for importing modules. It is asynchronous and can import named or default exports.
- export
- The ES Modules keyword for exporting values. Can be used for multiple `named` exports or a single `default` export.
- "type": "module"
- A field in `package.json` that tells Node.js to treat all `.js` files in the project as ES Modules.