JavaScript Modules and Scripts for Pythonistas (Summary)

You made it. Dev’s digest now runs in JavaScript. node digest.js logs/week-44.log 75 prints the same report as digest.py and adds the week to season.txt.

In this course, you learned how to:

  • Export what other files need and import it by its relative path, ./ and .js included
  • Tell a default import from a named one in braces, and import built-ins from node: names
  • Run the program at the entry file’s top level, with if (import.meta.main) for a self-check
  • Take the arguments from process.argv.slice(2) and convert the limit with Number()
  • Report a usage error with console.error() and process.exitCode = 1
  • Read with await readFile(path, "utf8"), split with text.trim().split("\n"), and append to a path built from import.meta.dirname

Drop the Habits That Misfire

As in Python, modules have their own scope and load once, arguments are strings, usage errors go to standard error, and relative readFile() paths start from the folder you run node in. Wrong output often comes from NaN, a promise, or an argument that’s one position off. These habits misfire:

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.