Sharing a Setting Between Modules

Dev’s digest now finds its plant list from any folder. Next, --verbose has to reach readings.js, so it can report the science block’s skipped line. Python’s digest.py does that by setting another module’s global, settings.verbose, and every module that reads it sees the change. If the flag never arrives, Dev never learns the science block lost a reading.

As with Python’s sys.modules, a module runs once, the first time anything imports it, and every import that resolves to the same URL shares it. The difference is who may assign to its variables.

Change the Setting Through Its Owner

Python’s digest runs settings.verbose = "--verbose" in args, and readings.py reads settings.verbose. To try the port, work in ~/boiler-digest/boiler, where settings.js declares the flag beside its plant-loading code:

Language: JavaScript Filename: settings.js
export let verbose = false;

Save the natural port as check-assignment.js. process.argv.slice(2) corresponds to Python’s sys.argv[1:]:

Language: JavaScript Filename: check-assignment.js
import * as settings from "./settings.js";

const args = process.argv.slice(2);
settings.verbose = args.includes("--verbose");

Running node check-assignment.js --verbose gives this error. The stack trace is wrapped for display:

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

Become a Member to join the conversation.