loading...

re: What are CJS, AMD, UMD, and ESM in Javascript? VIEW POST

FULL DISCUSSION
 

Can you please clarify?

When CJS imports, it will give you a copy of the imported object.

Let's say we have three files:

// obj.js
module.exports = {
  a: 42
};

// bar.js
const fooObj = require('./obj');

console.log('obj from bar', fooObj);

// index.js
const obj = require('./obj');

obj.a = 50;
console.log('obj', obj);

require('./bar');

Console output after run node index.js:

obj { a: 50 }
obj from bar { a: 50 }

It's clear that index.js and bar.js share the same object from obj.js.

 

Not like you should be changing modules anyways...

code of conduct - report abuse