CJS and ESM — can’t we all just get along?

Photo by Stillness InMotion on Unsplash
const doSomething = require('./doSomething.js');
module.exports = function doSomething(n) {
// do something
import {foo, bar} from './myLib';...export { ... }

Are CJS and ESM just different syntaxes for the same thing?

console.log(1);const { b } = require('./b');console.log(2);
console.log(1);import { b } from './b';console.log(2);
  • ESM modules automatically add ‘use strict
  • They allow top level “await”
  • The exported values share a memory space with the imported values. In CJS the imported values are copies of the exported ones.
  • You can’t use variables in import statements, because the variables are not yet instantiated. (Unless you use dynamic imports)

So… can they just get along?


  1. https://redfin.engineering/node-modules-at-war-why-commonjs-and-es-modules-cant-get-along-9617135eeca1 (Focuses on the differences between CJS and ESM, how to reconcile them)
  2. https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/ (A deep dive into what are modules and how ESM works under the hood)
  3. https://irian.to/blogs/what-are-cjs-amd-umd-and-esm-in-javascript/ (Short summary of the differences between these and other module types)




Software developer @ riseup. MA student @ The Cohn Institute in Tel Aviv University

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Undefined — GatsbyJS — TypeError

邹明潮 Notes: YDKJS Prototype


5 VS Code Extensions You Need to be Using in 2021

How to structure your react app.

Working The System

Managing Data with Node.js Streams

Check out what Styled Components can do for your app

Leetcode (9): Palindrome Number

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dotan Reis

Dotan Reis

Software developer @ riseup. MA student @ The Cohn Institute in Tel Aviv University

More from Medium

A Few Words on Node.js & An Example of How to Use NPM’s ‘readline-sync’ Package

Debugging with webstorm

Lets get user inputs using inquirer and Yargs — Creating a npm command line Utility — Part 2

My Phase 4 Project