Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Watch Modules Load and Run Once

In this video, you watched Node load three of Dana’s files and saw that a module runs once and every file that imports it shares its exports.

Key Points

  • When you run node main.js, Node starts with main.js. Before any line of it runs, Node finds every import, follows the imports of those files too, and checks that each imported name is really exported.
  • A module that two files import is loaded only once. Node runs the imported modules first, then main.js last. That’s why "budget.js runs" appears once, at the top of the output.
  • Watch out: because every import is found and checked first, importing a name that isn’t exported stops the whole program with SyntaxError: ... does not provide an export named .... No file runs, not even budget.js.
  • An import gives you a name for the module’s exported object, not a copy. main.js and report.js each have a name, budget, and both reach the same object. When main.js raised the fun limit, report.js saw the raised limit too. The change lasted only for that run, and budget.js itself still says fun: 150.
  • It works like two variables in one file that reach one object. That’s as far as the comparison goes, and it’s all you need about imports for now. A what-if budget that’s really separate needs its own copy, which you’ll build later in this course.
  • Sharing is also why budget.js is worth having, since every file that imports it sees the same limits.

The Final Code

This toy version of the tracker has three short files. budget.js logs a line when it runs and exports Dana’s budget:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Dana’s tracker now lives in more than one file. In this video, you’ll watch what Node does when it loads those files, and how often each one runs.

00:10 Dana also keeps a monthly budget, with a limit for each category. It moves into budget.js, a module of its own. The file logs a line when it runs, and it exports one object, the budget.

00:24 This is a toy tracker, with three short files. report.js imports budget and works out how much is left to spend in a category. main.js imports the budget too.

00:36 It also imports the function from report.js. So two files import the same module.

00:44 Dana wonders how her month would look with more room for fun. So in main.js, she raises the fun limit from 150 to 200, and logs it. Then she asks report.js how much fun money is left, after the 38 dollars she spent on a movie night with Malik.

01:02 A reasonable guess is that each file gets its own copy of the budget. If that’s true, the raise stays in main.js, and report.js still works with 150, so 112 would be left.

01:16 Your turn: pause the video and predict how much fun money report.js says is left.

01:28 Fun left is 162, not 112. report.js used the raised limit of 200.

01:37 And budget.js logged its line only once, even though two files import it. Nothing crashed or warned her.

01:47 You ran node main.js, so main.js is where Node started, before anything else. Before running a line, Node read the imports of main.js and found the two files they name.

02:02 report.js has an import of its own, and it names budget.js again. Node already has that file, so it doesn’t load it a second time. Next, Node checked that every imported name is really exported.

02:16 Only then did it run the files, the imported ones first. budget.js ran once. It logged its line and made one budget object. That’s why its line came first.

02:30 Then report.js ran, with nothing to log, and main.js ran last. Watch out, though. Because every import is found and checked first, importing a name that isn’t exported stops everything.

02:45 You get the last lesson’s error, and no file runs, not even budget.js.

02:51 So there’s one budget object. main.js and report.js each have a name for it. When main.js changed the fun limit, it changed that one object, the one every file uses.

03:05 report.js read the same object, so it saw 200. Her what-if never stayed in main.js. The raise lasts only for this run, and budget.js itself still says 150.

03:20 The same happens with two variables in one file. In the REPL, limits is an object, and plan is a second name for it. Set fun through plan, and limits shows 200 as well.

03:33 An import works the same way across files. It gives you a name for the object, not a copy. That’s as far as the comparison goes, and it’s all you need about imports for now.

03:46 For now, Dana takes the raise back out of main.js, so report.js works with 150 again. Run it again, and report.js says 112 is left.

03:58 A what-if budget that’s really separate needs its own copy, and you’ll build one later in this course. That’s all you need about copies for now. Sharing is also why budget.js is worth having, since every file that imports it sees the same limits.

04:15 The rule is that a module runs once, and every file that imports it shares the same exports.

04:22 Next, in Give Each File One Job, you’ll decide what each of Dana’s files is for, and what it should export.

Become a Member to join the conversation.