Home/Tutorials/Node.js/CommonJS and ESM

0 of 3 saved

Node.js5 min read

CommonJS and ESM

Read this chapter, write the program on this page, then pass a short check to open the next one.

require vs import.

CommonJS and ESM

Older Node code uses CommonJS (require/module.exports). Modern Node supports ES modules (import/export). Know both when reading docs.

  • "type": "module" in package.json enables ESM.
  • require is sync; ESM is static and tree-shake friendly.
  • Stick to one style per project.
const message = "cjs-or-esm";
console.log(message);

After you read this chapter, use Write (when available) and Quiz on this page to lock it in.