In Python, the boiler package’s __init__.py imports four public functions and lists them in
__all__, and the caller writes import boiler. The JavaScript port has the same folder of modules,
with their exports.
First, write the folder’s front door yourself, the index file boiler/index.js:
export { readLog, summarise } from "./readings.js";
export { loadPlants } from "./settings.js";
That’s a re-export: an import in __init__.py and its name in __all__, in one line. Python’s
controller loader isn’t ported yet, so the parsers are still imported directly.
A namespace import collects a module’s exports under one name, so the natural port points it at the folder:
import * as boiler from "./boiler";
console.log(boiler);
It stops before that line runs. The error’s message and hint read as follows, wrapped for display. The rest of the trace is omitted:
Error [ERR_UNSUPPORTED_DIR_IMPORT]:
Directory import '/Users/you/boiler-digest/boiler'
is not supported resolving ES modules imported from
/Users/you/boiler-digest/digest.js
Did you mean to import "./boiler/index.js"?
On Windows, the paths begin with your user folder on the C drive. Where Python runs a package’s
__init__.py, an ES module import names one file, and a folder isn’t a file. Now the caller names
the file:
import * as boiler from "./boiler/index.js";
console.log(boiler);
The namespace contains the three forwarded functions:
[Module: null prototype] {
loadPlants: [AsyncFunction: loadPlants],
readLog: [AsyncFunction: readLog],
summarise: [Function: summarise]
}
index is a convention, and the hint found that file in the folder, but the import never falls back
to it. That’s where the Python analogy ends: you name the index file yourself.
A re-export can rename, the way as does in a Python import. This line offers Halden’s parse to
callers as parseHalden, and the Halden module keeps its own name:
export { parse as parseHalden } from "./controllers/halden.js";
A re-export forwards a name to callers without creating a local name inside the index file. In
__init__.py, an import creates one. So console.log(summarise) after export { summarise } from
"./readings.js" throws ReferenceError: summarise is not defined, and the file stops. If the index
file needs it, import it there too:
import { summarise } from "./readings.js";
export { summarise } from "./readings.js";
console.log(summarise);
A star re-export, export * from "./readings.js", forwards all of a module’s named exports. The rule:
name the file, then choose what it exposes.
Next up: Give Asha One Place to Import the Parsers, an exercise. You’ll give her dashboard widget one place to request its parser functions.