In ES6 modules, this works as expected:
- index.js
import { foo, init } from "./foo";
init();
console.log(foo); // 42
- foo.js
export let foo;
export const init = () => {
foo = 42;
};
However in Node.js, when I tried the same thing, the foo didn't get updated.
How can I make the foo updates to 42 after init() is run?
- index.js
let { foo, init } = require('./foo');
init();
console.log(foo); // undefined
- foo.js
let foo;
const init = () => {
foo = 42;
};
module.exports = {
init,
foo
};
I know I could export a function getFoo() and returns the updated foo, but it seems not very convenient.
What's the proper way to export a variable in Node.js that may change after?