Node.js CommonJS vs ES Modules Explained

Understand CommonJS and ES modules in Node.js, when to use each one, and how to avoid import/export mistakes in backend projects.

By Anderson Alvarez Vásquez · 2026-11-21

  • nodejs
  • javascript
  • modules
  • backend

Node.js Module Systems: CommonJS vs ES Modules Node.js has two module systems that developers use every day: CommonJS and ES modules . They solve the same problem, but they do it in different ways. If you have ever seen require in one project and import in another, you have already run into this difference. Understanding the module system matters because it affects how you structure files, how tools load code, and why some snippets work in one project but fail in another. The short version text CommonJS = older Node.js module system ES modules = modern JavaScript module standard Both let you split code into reusable files. The important difference is the syntax, loading behavior, and interoperability rules. CommonJS in Node.js CommonJS is the classic Node.js module format. It uses require to load code and module.exports to expose code from a file. Example js // math.js function add a, b { return a + b; } function multiply a, b { return a b; } module.exports = { add, multiply, }; js // app.js const { add, multiply } = require './math' ; console.log add 2, 3 ; console.log multiply 2, 3 ; CommonJS is still very common in existing Node.js applications, especially older codebases and many packages on npm. Why CommonJS feels simple - It works by default in many Node.js projects. - It is easy to read. - It fits naturally with older tutorials and packages. CommonJS tradeoff CommonJS is mature and reliable, but it is not the modern JavaScript module standard used by browsers and newer tooling. ES modules in Node.js ES modules, often called ESM, are the standardized JavaScript module system. They use import and export . Example js // math.js export function add a, b { return a + b; } export function multiply a, b { return a b; } js // app.js import { add, multiply } from...