Accessing variables through modules - Node.js

Hello, could someone provide me a link to the article or video about accessing variables in Node js and their scopes? What I mean: I have a problem with undestanding when I have access to each variable declared in each module. For example I have an app.js main file and I’m exporting a file1.js to it but I’m also exporting file2.js into file1.js. And I don’t know when I declare a variable in files2.js if I will have access to it in or app.js? Also the same situation when I will declare something in app.js, where I will have access to it? So could someone give me some resource where I can learn something more about it?

Modules are self contained units of code, they aren’t supposed to leak. If you want access to a variable, you need to export that variable.

There aren’t really resources per se specifically for this: it’s just the point of modules (any learning resource would just be “using modules in JS”). You write some code. If there is anything you want exposed (ie allow other modules to use it), you have to export it. Anything you don’t export is just internal to the module.

1 Like