// counter.mjs
export let count = 0;
export function inc() {
count++;
}
// main.mjs
import { count, inc } from "./counter.mjs";
inc();
console.log(count);
ESM logs 1, CommonJS logs 0
The answer is "ESM logs 1, CommonJS logs 0". ES module imports are live bindings. This means the imported name is a link to the exporting module's variable, not a copy. So after inc(), the importer sees 1.
In CommonJS, exports.count = count copies the number 0 at that moment. Destructuring in require copies it again. So later increments inside the module are not seen.
The tempting option "ESM logs 1, CommonJS logs 1" is wrong because CommonJS exports a copy of a number, not a live link.
Read more