I am following a tutorial and it says
ES modules uses live bindings. It means a feature to support cyclical dependencies.
But I don't clearly understand this concept. What does this mean?
Live bindings is a concept introduced in ES modules. It means that when the exporting module changes a value, the change will be visible from the importer side. This is not the case for CommonJS modules. Module exports are copied in CommonJS. Hence importing modules cannot see changes happened on the exporter side.
export let count = 1;
export function increment() {
++count;
}
import { count, increment } from './counter.mjs';
console.log(count);
increment();
console.log(count);
$ node --experimental-modules index.mjs
1
2
let count = 1;
function increment() {
++count;
}
exports.count = count;
exports.increment = increment;
const { count, increment } = require('./counter.js');
console.log(count);
increment();
console.log(count);
$ node index.js
1
1
More resources on the topic:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With