Choosing What a Module Shares

Your digest.js already imports parseLine() from "./readings.js". Porting the rest of readings.js means deciding which of its functions Asha may use, a question Python never made Dev answer. Python lets Asha import any top-level name, but in JavaScript the module decides.

Export What Other Files Need

Python’s from readings import _is_blank works for any top-level name. The underscore marks _is_blank as internal only by convention and, like __all__, affects only from readings import *. Both languages give each module its own scope, but a JavaScript import sees only the names marked with export:

Language: JavaScript Filename: readings.js
export function isBlank(line) {
  return line.trim() === "";
}

export function parseLine(line) {
  const [time, temp] = line.trim().split(",");
  return { time, temp: Number(temp) };
}

export function summarise(readings, threshold) {
  // The loop that finds the peak and the readings over the limit
}

Dev exports all three because digest.js needs them, including isBlank() to skip blank lines. Those are the only functions that other files, such as Asha’s, can import. A helper that only parseLine() calls would stay private.

Python’s three import forms map onto braces and as:

Python JavaScript
from readings import is_blank, parse_line import { isBlank, parseLine } from "./readings.js";
from readings import summarise as summarise_readings import { summarise as summariseReadings } from "./readings.js";
import readings import * as readings from "./readings.js";

Unlike Python’s, these imported names are read-only, so assigning to isBlank or readings.isBlank throws a TypeError, though the values aren’t frozen.

Python runs an import when it reaches the line, even inside a function. JavaScript allows import declarations only at a file’s top level and loads all the imported modules before the first line runs.

That’s why a missing export bites early. Forget the export on isBlank(), since Python never asked for one, and digest.js stops with SyntaxError: The requested module './readings.js' does not provide an export named 'isBlank' before any of its code runs. Add the export, or drop the name from the import if the helper should stay private.

Rename or Take the Whole Module

An imported name can clash with one your file already declares. Asha’s sports hall script has its own summarise(), so she writes import { summarise as summariseReadings } from "./readings.js";. Without the as, Python’s later def would replace the import without warning, but JavaScript refuses to load the file with SyntaxError: Identifier 'summarise' has already been declared.

Despite the *, import * as readings corresponds to Python’s import readings, not from readings import *. The as name is required. Python names the variable after the module, but "./readings.js" is a path string, not a name, so you choose the name yourself:

Language: JavaScript Filename: digest.js
import * as readings from "./readings.js";

console.log(readings.parseLine("Wed 07:00,76.5"));
console.log(readings);

Calls use dot access, and readings is a module namespace object holding exactly the exports, sorted by name:

Language: Shell
$ node digest.js
{ time: 'Wed 07:00', temp: 76.5 }
[Module: null prototype] {
  isBlank: [Function: isBlank],
  parseLine: [Function: parseLine],
  summarise: [Function: summarise]
}

You can ignore the null prototype label for now.

Read a File’s Imports

JavaScript developers export only what other files need and prefer named imports. digest.js keeps import { isBlank, parseLine, summarise } from "./readings.js";, so you can see at the top which three functions it uses and where they come from.

A mistyped name in the braces stops the file as it loads, like the missing isBlank export. With import * as readings, a typo such as readings.sumarise() fails only when that line runs, raising TypeError: readings.sumarise is not a function. Python would raise an AttributeError at the same point.

As in Python, a module is loaded once and shared by every file that imports it. A console.log("readings.js runs") at the top of readings.js prints one line even when two files in the run import it.

In the next lesson, Importing Defaults and Built-In Modules, you’ll import the dashboard developers’ format.js and its one default export, and bring in modules that come with Node itself.

Become a Member to join the conversation.