Example 1. I have a file test.js
const lib = {
foo: () => console.log(a)
};
lib.foo(); // can't access "a" before init
const a = 3;
Example 2. I have two files: test1.js and test2.js
const lib = require('./test2');
lib.foo(); // 3
const lib = {
foo: () => console.log(a)
};
const a = 3;
module.exports = lib;
lib.foo()afterconst amodule.exports.foo = () => console.log(a); const a = 3;where you export the function before initialising the constant. All that matters is that it's not called beforeais initialised.