Skip to content

Chapter 2 of 8

Module Systems

Node.js supports two module systems for organizing code. CommonJS is the original, using the require function to load modules synchronously and module.exports (or the shorter exports reference) to expose values. Each file is treated as its own module with its own scope, and modules are cached after the first load. This synchronous loading model is simple and works well on the server, where files are local and disk access is fast.

ES Modules (ESM) are the official JavaScript standard, supported in Node.js 12 and later. ESM uses the more familiar import and export syntax, is asynchronous, and supports top-level await. ESM can be enabled by adding "type": "module" to package.json or by giving files the .mjs extension. CommonJS, by contrast, is the default for .js files in projects that have not opted into ESM, and CommonJS modules can also be written explicitly using the .cjs extension.

The two systems differ in several important ways. CommonJS loads synchronously while ESM is asynchronous. CommonJS wraps each module in a function, whereas ESM runs in strict mode by default. Only ESM allows top-level await, which simplifies code that needs to wait for resources during initialization. Both systems cache modules, but ESM uses live bindings: if the exporting module updates a value, importing modules see the new value automatically. Choosing between them depends on the project, but modern Node.js code increasingly favors ESM for its alignment with the broader JavaScript ecosystem.

All chapters
  1. 1Foundations of Node.js and the Event Loop
  2. 2Module Systems
  3. 3Streams and Buffers
  4. 4Asynchronous Programming
  5. 5Building Web Applications with Express
  6. 6Core Built-in Modules
  7. 7Package Management and Project Configuration
  8. 8Error Handling, Process Management, and Events

Drill it

Reading is not remembering. These come from the Nodejs Runtime deck:

Q

What is Node.js?

Node.js is a JavaScript runtime built on Chrome's V8 engine. It allows running JavaScript outside the browser, using an event-driven, non-blocking I/O model tha...

Q

What is the Node.js event loop?

The event loop is the mechanism that allows Node.js to perform non-blocking I/O operations despite JavaScript being single-threaded. It continuously checks the...

Q

What are the phases of the Node.js event loop?

Timers – executes setTimeout and setInterval callbacksPending callbacks – I/O callbacks deferred to next iterationPoll – retrieves new I/O eventsCheck – execute...

Q

What is the difference between setImmediate() and setTimeout()?

setImmediate() executes in the check phase of the event loop, after the poll phase completes. setTimeout(fn, 0) executes in the timers phase on the next iterati...