Reading CommonJS Code

Dev’s digest can choose a controller by name. Halden Heating has now sent its own fault code file, including F40 for ignition lockout, a code the table copied from the manual doesn’t contain. Before connecting that file to the digest, you need to understand what it makes available and how Halden’s service tools load it.

Python modules use one import system. Node.js supports both the ECMAScript (ES) modules in your project and CommonJS, an older module system you’ll encounter in existing packages and service tools. You’ll read Halden’s CommonJS here while keeping your own project’s files as ES modules.

Read the Exported Object

In Python, Halden could provide a module named halden_faults.py containing this table and function:

Language: Python Filename: halden_faults.py
FAULTS = {
    "F12": "low water pressure",
    "F21": "flue sensor fault",
    "F33": "pump overrun",
    "F40": "ignition lockout",
}

def describe(code):
    return f"{code} ({FAULTS.get(code, 'unknown fault')})"

A Python caller selects the name it needs from that module:

Language: Python Filename: read_fault.py
from halden_faults import describe

print(describe("F40"))

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.