Reading CommonJS Code

Halden Heating has sent its own fault code file, with F40 for ignition lockout, a code missing from the table Dev copied from the manual. Python has one import system, but Node has two, and Halden’s file uses the older one, CommonJS, which you’ll also meet in legacy packages and answers online. You’ll learn to read it here while your own files stay ECMAScript (ES) modules.

Read What the File Exports

Make a reading folder and enter it. These commands work in a macOS or Linux terminal and in Windows PowerShell:

Language: Shell
$ mkdir ~/halden-reading
$ cd ~/halden-reading

Save a reading copy of Halden’s file there as halden-faults.cjs, since a filename ending in .cjs marks a file as CommonJS:

Language: JavaScript Filename: halden-faults.cjs
const FAULTS = {
  F12: "low water pressure",
  F21: "flue sensor fault",
  F33: "pump overrun",
  F40: "ignition lockout",
};

function describe(code) {
  const meaning = Object.hasOwn(FAULTS, code)
    ? FAULTS[code]
    : "unknown fault";
  return `${code} (${meaning})`;
}

module.exports = { describe, FAULTS };

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

Become a Member to join the conversation.