Importing Across the Package

Dev’s Halden parser needs the fault descriptions stored one folder above it. The digest also needs the package’s front door, and the log reader needs the settings. With three heating plants sharing these modules, you need imports that still point to the right files as the project grows.

The front door is boiler/index.js: it re-exports the functions the digest uses. Inside the package, each file can import the module it needs directly. Your next decision is how to describe that module’s location.

Reach a Neighboring Module

In Python, boiler/controllers/halden.py uses an absolute import, the form PEP 8 recommends:

Language: Python
from boiler.faults import describe

print(describe("F12"))

Python finds boiler on its module search path. You can also write from ..faults import describe inside this package: the second dot moves up from controllers to boiler.

In JavaScript, a relative import path locates a file from the importing file’s folder. Keep the existing boiler/faults.js, which exports describe(), and the parser in boiler/controllers/halden.js. Save this separate check beside the parser as boiler/controllers/check-faults.js:

Language: JavaScript Filename: boiler/controllers/check-faults.js
import { describe } from "../faults.js";

console.log(describe("F12"));

Run node boiler/controllers/check-faults.js from your ~/boiler-digest folder, and the existing fault table supplies the description:

Language: Program Output
F12 (low water pressure)

The ../ moves up one folder, and then faults.js names the file there. Each additional ../ moves up another folder. The familiar ./ stays in the importing file’s folder, and the .js extension remains part of the name.

The path is relative to check-faults.js, wherever you launch the program from. Running the digest from the project folder doesn’t make "./faults.js" point into boiler: written in halden.js, it would point into boiler/controllers.

Watch out: carrying Python’s absolute import across as "boiler/faults.js" gives ERR_MODULE_NOT_FOUND because Node.js reads a name without ./ or ../ as an installed package called boiler.

Relative paths suit neighboring files, but a chain such as "../../readings.js" ties an import to the importing file’s depth. Moving that file into another folder means updating the chain. Python’s absolute imports avoid that dependency on depth. Your JavaScript project can give imports a similarly stable starting point.

Name Modules Across the Project

A package imports map assigns project-wide names to files. In your project’s package.json, keep "type": "module", which makes its .js files ECMAScript modules, and add "imports":

Language: JSON Filename: package.json
{
  "type": "module",
  "imports": {
    "#boiler/*": "./boiler/*"
  }
}

The * stands for the matching part of the name. For "#boiler/faults.js", that part is faults.js, so the target is "./boiler/faults.js". Here, the target starts at the folder containing package.json, rather than at the importing file.

Replace the import line in boiler/controllers/check-faults.js with the mapped import, keeping the console.log() call that prints the result:

Language: JavaScript Filename: boiler/controllers/check-faults.js
import { describe } from "#boiler/faults.js";

console.log(describe("F12"));

Running the same command prints the same description:

Language: Program Output
F12 (low water pressure)

The # is required: a map key such as "boiler/*" is ignored, leaving "boiler/faults.js" to be treated as an installed package name. Keep the filename’s .js extension too. These names work inside this package, whose boundary is defined by its package.json. A published package uses "exports" to declare what other projects may import.

In the existing boiler/controllers/halden.js, change only the import line to import { describe } from "#boiler/faults.js";. Keep its parse() export and all other code so the digest can still import its parser.

Your digest can now import the package’s front door as import * as boiler from "#boiler/index.js", and readings.js can import its setting as import { verbose } from "#boiler/settings.js". Use ./ and ../ between neighbors and #boiler/... across the project. For example, if you move check-faults.js from boiler/controllers to the project root, its relative import becomes "./boiler/faults.js". Its mapped "#boiler/faults.js" import stays unchanged.

In the next lesson, Finding Files Next to a Module, you’ll locate the plant list beside the settings module and choose how to load that data.

Become a Member to join the conversation.