🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Untitled Lesson

Total XP: 0|💻 backend XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Omitting the file extension in a relative import under native ESM

// Wrong — throws ERR_MODULE_NOT_FOUND import { db } from "./database"; // Correct import { db } from "./database.js";

The Solution //

Node's native ESM resolver, unlike bundler resolvers (webpack, Vite), does not guess file extensions — it requires the exact extension in every relative import specifier. Add the .js (or .mjs) extension explicitly to every local relative import.

The Error //

Using __dirname or __filename inside a file after switching to "type": "module"

// Wrong — ReferenceError: __dirname is not defined const configPath = path.join(__dirname, "config.json"); // Correct import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; const __dirname = dirname(fileURLToPath(import.meta.url)); const configPath = join(__dirname, "config.json");

The Solution //

These CommonJS-only globals are undefined in ES Modules and throw a ReferenceError. Reconstruct the equivalent path using import.meta.url passed through node:url's fileURLToPath(), combined with node:path's dirname().

Continue Learning