Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
Which keyword is used in the legacy CommonJS module system to import another file or NPM package into the current file?
π» Code Challenge | +75 XP
Write the Node.js/Backend code snippet to implement a CommonJS vs ES Modules pipeline. Include the setup and basic execution steps.
You are reviewing a CommonJS vs ES Modules pipeline and the output is incorrect. Reorder the following pipeline stages in the correct logical order to fix the bug: Input Data, Process, Output.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
The Error //
Omitting the file extension in a relative import once a project uses ESM
// Wrong in ESM: throws ERR_MODULE_NOT_FOUND
import db from './db';
// Correct: extension is mandatory
import db from './db.js';The Solution //
CommonJS's require('./db') auto-resolves to db.js, but Node's ESM resolver has no such fallback and throws ERR_MODULE_NOT_FOUND if the extension is missing. Once package.json has "type": "module" (or a file uses .mjs), every relative import must include the explicit .js extension.
The Error //
Using __dirname or __filename in an ESM file and getting 'ReferenceError: __dirname is not defined'
// Wrong in ESM
console.log(__dirname); // ReferenceError
// Correct
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);The Solution //
Node only injects __dirname and __filename as magic globals in CommonJS files β they simply don't exist in the ESM module scope. Reconstruct the equivalent path manually from import.meta.url using the built-in url and path modules.