Loading video player…

Turning a Package Into a Folder of Modules

A JavaScript folder of modules uses an explicit entry file. In boiler/index.js, named re-exports choose the public functions while their implementations stay in the existing modules:

Language: JavaScript
export { readLog, summarise } from "./readings.js";
export { loadPlants } from "./settings.js";

The caller names that file, including its extension:

Language: JavaScript
import * as boiler from "./boiler/index.js";

index.js is a convention. The ES module import does not search a folder for it. A re-export can choose a public alias; if the entry file also uses the function locally, it needs an ordinary import too.

Next, complete Exercise: Give Asha One Place to Import the Parsers to give Asha’s dashboard widget one place to request its parser functions.

00:00 You’ll give the school’s digest one place to import its tools. The main boiler, sports hall, and science block keep their existing parsers. In Python, the caller imports the package, and the package’s initialization file offers its public functions.

00:15 The JavaScript port already has working modules that export their functions. You need the same clear front door.

00:24 In Python, importing the folder lets the caller use the package’s functions. In JavaScript, a namespace import collects a module’s exports under one name. The natural port points that import at the boiler folder.

00:42 Will the visible JavaScript import reach the line that prints the namespace? Your turn: pause the video and predict whether that line runs.

00:57 It stops before that line runs. The error says the directory import is unsupported and suggests an index file. Ten stack frames are omitted here. On Windows, these paths begin with your user folder on the C drive.

01:14 Python finds the package’s initialization file; this ES module import needs an explicit file path.

01:23 Create the entry module inside the boiler folder. In Python, these imports put the readings functions on the package. In JavaScript, this line passes those existing exports through the new file.

01:36 That is a re-export. The second line offers the settings module’s plant loader through the same front door. Its implementation stays in its original module.

01:47 Python offers four functions, including its controller loader; this JavaScript entry forwards these three tools. The parser imports stay as they are.

02:01 Now the JavaScript caller names the entry file, including its extension. The namespace contains the three functions forwarded at this stage. The name index is a convention; the import doesn’t search for it inside a folder.

02:17 That is where the Python analogy ends. You keep a public front door, but you name its file yourself.

02:26 In Python, the initialization imports make these functions package attributes. Its export list controls a star import, rather than creating those attributes.

02:36 In JavaScript, the entry module’s named re-exports select what its namespace exposes. The existing readings, settings, and controller files remain separate.

02:48 The main boiler, sports hall, and science block still use the same working code.

02:56 You can also rename a public export at that front door. Python’s import alias gives the source parser a distinct name. In JavaScript, the re-export’s alias does the same naming job for callers.

03:08 The source still exports parse; this entry offers parseHalden. That lets a caller request the public name without changing the Halden parser itself.

03:22 A re-export forwards a name to callers without creating a local name inside the entry module. Reading that name there produces a ReferenceError.

03:32 If the entry needs to use it, import it there too. A star re-export forwards all named exports. The selected names keep this front door’s purpose clear.

03:47 The JavaScript digest now uses the same tools for all three plants through one explicit entry file. The readings and settings implementations stay where they belong.

03:57 The rule is name the file, then choose what it exposes. Next up is 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.