JavaScript Modules: A Beginner’s Guide
Preethi Kasireddy
1.5K76
Good article. One note, the examples you shared comparing the CommonJS binding vs. the ES6 module aren’t demonstrating what they are describing. When setting `counter: counter`, the original `counter` variable is a primitive copied by value, and the increment/decrement methods are changing the variable, not the value assigned to the `counter` property. If you change the methods to something like`module.exports.counter++`, you should see the value mutated in the importee.
Rather, to compare “live” vs. “static” modules, a good comparison would be to load the module via two other files, showing that mutating the value in one file does or does not propagate based on the module system you are using.
Looking forward to the next post!