Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Using a CommonJS File in Your Project

Dev copied Halden’s supplied fault-code file into boiler/ as halden-faults.js, and the controller imports from it by name, as you’d import from a Python module copied into a package:

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

The file ends with module.exports = { describe, FAULTS };, yet node digest.js logs/week-12 stops before printing anything, as the digest loads the main boiler’s Halden controller. The error line of the trace reads, wrapped for display:

Language: Program Output
SyntaxError: The requested module
'#boiler/halden-faults.js' does not provide
an export named 'describe'

Node picks each file’s module system from its name and its package: an .mjs file is an ES module, a .cjs file is CommonJS, and a .js file follows the "type" in the nearest package.json. In Halden’s project, with no type, .js files are CommonJS; this project says "module", so Halden’s module.exports line is ordinary code, not an export. Python has one module system, so it never makes this choice.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The welcome video showed two lines: the end of Halden’s supplied file, and the controller’s import of describe from it. Now you’ll connect that file to the digest, as it is.

00:10 In Python, a module copied into a package works: this caller prints F40, ignition lockout. In JavaScript, Dev copied the whole file into the boiler folder: the same table and function, plus the exported object you’ve already read.

00:27 It keeps its .js suffix.

00:32 The controller imports describe by name, and the package file says the project’s .js files are ES modules. The command runs the digest on week twelve.

00:42 Can the digest obtain the description from Halden’s copied file? Your turn: pause the video and predict the result.

00:55 The import fails: the requested module doesn’t provide an export named describe.

01:01 Nothing has printed: the error comes as the digest loads the first plant’s Halden controller.

01:09 The filename decides, with its package: an .mjs file is an ES module; a .cjs file is CommonJS. For .js files, Node checks the nearest package file’s type.

01:22 In Halden’s project, with no type, they’re CommonJS; here it says module. So Halden’s module.exports line is ordinary code, not an export: the import finds no named export.

01:36 Python, with one system, never makes this choice.

01:41 Run the file on its own, and it says “module isn’t defined in ES module scope”. Node advises renaming it to use the .cjs extension.

01:51 A Python import runs the module first; this import fails before the file runs; the direct run reaches the assignment.

02:01 Keep Halden’s code as supplied. Rename the file to end in .cjs. Then update the import path in the controller to match. The package keeps its module setting: only the supplied file is CommonJS.

02:16 The digest now reports all three plants, with the main boiler’s F12, low water pressure. A separate call shows Halden’s F40 comes with the file.

02:28 A default import from CommonJS receives the whole exported value: Halden’s object, with the function and fault table. The existing named import works too, because Node can find these names by reading the file’s source.

02:43 Watch out: when code builds the exported object up step by step and Node can’t find the names, use the default import instead. The other way round, Node twenty-six lets CommonJS require() an ES module that has no top-level await.

03:00 The rule is to preserve supplied CommonJS as .cjs and import it from your own ES modules. In the next exercise, Translate Halden’s Service File, you’ll turn to Halden’s service file to supply the digest with each plant’s next service visit.

Become a Member to join the conversation.