JavaScript Modules and Scripts for Pythonistas (Summary)

Congratulations, you made it to the end of the course! Dev’s boiler log digest now runs in JavaScript with the command node digest.js logs/week-44.log 75. It prints the same report as digest.py, from Peak flow temperature: 78.0 and Readings over 75.0: 2 to the lines for Wednesday’s 76.5 and Thursday’s 78.0, and it adds the week’s line to season.txt for Halden Heating.

In this course, you learned how to:

  • Mark what a module shares with export, and import it by its path, ./ and .js included, as in import { isBlank, parseLine, summarise } from "./readings.js";
  • Tell a default import, import formatReading from "./format.js", from a named one in braces, and import Node’s built-in modules from names such as "node:fs/promises"
  • Let the entry file’s top level be the program, and keep a self-check behind if (import.meta.main) in a file that’s both a module and a script
  • Take the arguments with process.argv.slice(2) and convert the limit with Number()
  • Report a usage error with console.error(), set process.exitCode = 1, and let the script finish instead of calling process.exit()
  • Read a whole file with await readFile(path, "utf8"), and split it into lines with text.trim().split("\n")
  • Add to a file with appendFile(), write a whole one with writeFile(), and build a tool’s own paths from import.meta.dirname

Keep the Habits That Carry Over

Much of your Python thinking still holds. A module has its own scope and executes once per run, however many files import it. The entry file is the one you hand to node, as you hand one to python. Arguments arrive as strings, standard error is for messages, and an exit code of zero means success. A relative path you pass to readFile() or appendFile() starts from the folder you run in, and a newline at the end of a line is still your job.

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.