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;). __dirnameand__filenameare not defined in ESM; must be derived viaimport.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);