InterviewHubPRO
Section Overview
Q17

Question 17 of 60

Part 2: Runtime Internals, Libuv & The Event Loop (6 - 20)

28%
Node.js & Libuv•Part 2: Runtime Internals, Libuv & The Event Loop (6 - 20)

Operational pitfalls of mixing CJS and ESM.

In Simple Words:

  • ESM cannot import named exports from CJS without default import wrappers (import pkg from 'cjs-pkg'; const { fn } = pkg;).
  • __dirname and __filename are not defined in ESM; must be derived via import.meta.url.
javascript
import { fileURLToPath } from 'url';
import { dirname } from 'path';

// Emulating __dirname in ESM:
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);