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

Halden’s supplied fault-code file uses CommonJS. Keep its code intact, rename it halden-faults.cjs, and update the controller’s import path. Your own new files stay ES modules in the existing "type": "module" project.

An ES module can import CommonJS. A default import receives the complete module.exports value. Named imports work when Node can find the names in the CommonJS source; use the default import when it can’t.

The controller keeps its parser behavior and imports Halden’s existing description function:

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

The supplied file still exports both describe and FAULTS, including F40. The digest keeps the three plants, verbose diagnostic and optional chart.

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 You can read Halden Heating’s CommonJS file. Now you’ll connect it to the three-plant digest, while keeping your own files as ECMAScript, or ES, modules. In Python, a supplied module can sit inside the package.

00:16 Its table includes F40, ignition lockout, and this caller prints that description.

00:23 In the JavaScript project, Dev copied Halden’s original file into the boiler folder. It has the same table and function, plus the exported object you’ve already read.

00:34 The filename retains its .js suffix.

00:40 The JavaScript controller imports the description by name, as it did from Dev’s old table. The project’s package file still says its .js files are ES modules.

00:52 The command runs the ordinary digest on week twelve. Can the digest obtain the description from Halden’s copied file? Your turn: pause the video and predict the result.

01:09 The import fails before Halden’s file runs. JavaScript reports that the requested module doesn’t provide an export named describe. Nothing from the digest has printed: its first controller belongs to the main boiler.

01:26 The deciding detail is the filename, together with its package. An .mjs file is an ES module; a .cjs file is CommonJS. For .js files, Node checks the nearest containing package’s type.

01:43 Here, module selects ES modules. Halden’s exported-object assignment is ordinary code, not an ES module export declaration. So the import cannot find that named export.

01:58 The copied file’s CommonJS intent doesn’t override the filename and package decision. Python has no corresponding choice between these two module systems.

02:11 To see the same file’s evaluation error, run the vendor file on its own. The terminal command is shown here. This error excerpt says module isn’t defined in ES module scope.

02:25 Node also identifies the package’s type and advises the .cjs extension. The difference is evaluation. Importing by name rejected the missing export before this body ran.

02:39 Running the file directly reaches its CommonJS assignment. Both diagnostics point to the same module-system mismatch.

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

03:03 Your own new JavaScript files stay ES modules; only the supplied file needs the CommonJS boundary. The digest now reports all three plants. The main boiler’s original fault still says F12, low water pressure.

03:21 A separate call confirms that Halden’s additional F40 description comes with its file.

03:28 In an ES module, a default import from CommonJS receives the complete exported value. Here that value is Halden’s object, containing the description function and the fault table.

03:42 The existing named import works too: Node can find these names by reading this file’s source. Watch out: when code builds the exported object and Node can’t find the names, use the default import instead.

03:57 The reverse direction is available in Node twenty-six too: CommonJS can require() an ES module if its module graph has no top-level await.

04:07 For this project, your connection still goes from your ES module to Halden’s CommonJS file.

04:15 The rule is to preserve supplied CommonJS as .cjs and import it from your ES modules. Your own files keep the project’s existing module style.

04:26 The three plants, verbose diagnostic and optional chart retain their behavior. The vendor table keeps both exported names and all four fault codes. 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.