I found that this function could not access "arr" even though it is in the same scope
const arr = [1, 2, 3]
const foo = require("./test2")
// const foo = () => console.log(arr[0])
foo()
> ReferenceError: arr is not defined
// ***test2.js definition for reference***
module.exports = () => console.log(arr[0])
But when I declare the same function in the main module, it works just fine.
const arr = [1, 2, 3]
// const foo = require("./test2")
const foo = () => console.log(arr[0])
foo()
> 1
Both are identical functions, except one declaration is from a module, why is one able to access the global variable while the other isn't? Is it something in the way Node executes JS code? Can anyone explain this behavior?