Loading video player…

Turning a Package Into a Folder of Modules

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:

Language: JavaScript
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:

Language: JavaScript
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:

Language: Program Output
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:

Language: JavaScript
import * as boiler from "./boiler/index.js";
console.log(boiler);

The namespace contains the three forwarded functions:

Language: Program Output
[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:

Language: JavaScript
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:

Language: JavaScript
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.

00:00 You’ll give the school’s digest one place to import its tools. In Python, the package’s __init__.py imports four public functions and lists them in __all__, and the caller writes import boiler.

00:13 The JavaScript port has the same folder of modules, with their exports. First, write the folder’s front door yourself, an index file in boiler. Its first line passes the readings functions through, the second the plant loader.

00:29 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.

00:43 In JavaScript, a namespace import collects a module’s exports under one name, so the natural port points it at the boiler folder. Will the import reach the line that prints the namespace?

00:56 Your turn: pause the video and predict whether that line runs.

01:06 It stops before that line runs.

01:10 The error says the directory import is unsupported and suggests the index file. Ten stack frames are omitted here. On Windows, these paths begin with your user folder on the C drive.

01:22 Where Python runs a package’s __init__.py, an ES module import names one file, and a folder isn’t a file.

01:32 Now the caller names the file. The namespace contains the three forwarded functions. index is a convention, and the hint found that file in the folder, but the import never falls back to it.

01:45 That’s where the Python analogy ends: you name the index file yourself.

01:52 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.

02:04 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 reading it in the index file throws a ReferenceError, and the file stops.

02:19 If the index file needs it, import it there too, and the line logs it. A star re-export forwards all of a module’s named exports.

02:31 The rule: name the file, then choose what it exposes. Next up is the exercise Give Asha One Place to Import the Parsers. You’ll give her dashboard widget one place to request its parser functions.

Become a Member to join the conversation.