Importing Across the Package

Dev’s Halden parser, boiler/controllers/halden.js, needs describe() from boiler/faults.js, one folder up. Python’s halden.py uses an absolute import for it, as PEP 8 recommends. JavaScript has no sys.path for your own files, so you’ll choose between paths relative to the importing file and names that stay the same within the package.

Reach a Neighboring Module

A relative import path starts at the folder containing the importing file, and each ../ goes up one folder. Each extra dot in Python’s from ..faults goes up a package instead, so it works only when the file runs as part of the package, as with python -m. Keep halden.js as it is, and save a check beside it 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 ~/boiler-digest to display the fault description:

Language: Program Output
F12 (low water pressure)

The path is relative to check-faults.js, so it works from any working directory. As before, ./ is the file’s own folder, and the .js stays. If you port the absolute import as "boiler/faults.js", with no ./ or ../, Node looks for an installed package and stops with ERR_MODULE_NOT_FOUND and “Cannot find package ‘boiler’”.

Relative paths suit neighbors, but each depends on where its file sits. At the project root, check-faults.js would need "./boiler/faults.js".

Name Modules Across the Project

The "imports" field in package.json defines Node’s subpath imports by mapping names that start with # to files. Keep "type": "module" and add the map:

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

The * matches the rest of the name, and Node resolves targets relative to the folder holding package.json. Switch check-faults.js to the mapped name:

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

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

The same command prints the same line:

Language: Program Output
F12 (low water pressure)

Node ignores a key without the #, such as "boiler/*", and the ERR_MODULE_NOT_FOUND error comes back. Keep the .js too. The * copies the name as is, so "#boiler/faults" finds no file. A published package’s "exports" field, by contrast, says what other projects may import.

Python’s two import styles map like this:

Python JavaScript
from ..faults import describe import { describe } from "../faults.js";
from boiler.faults import describe import { describe } from "#boiler/faults.js";

The second row is only an approximate match: #boiler/ is a name you declare, found through the nearest package.json rather than sys.path, so it works only in files that share that package.json. A folder with its own package.json starts a new scope without the map.

In halden.js, change only the import line to the # form. Switch digest.js to import * as boiler from "#boiler/index.js"; and readings.js to import { verbose } from "#boiler/settings.js";.

Use ./ and ../ between neighbors and #boiler/... across the project, where an importing file can move within the package without edits.

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 it.

Become a Member to join the conversation.