Skip to content
Nilay Kabariya

Home · Topic · 8 entries

ES modules vs CommonJS errors: fixes that work

Most "module" errors come from the same split: ES modules (import) and CommonJS (require) follow different rules. Newer Node versions changed several of those rules, so the entries below show what still fails today and the fix that works.

Entries

  1. No.026

    Fix: Dynamic require of "react" / "fs" is not supported

    esbuild's ES module output throws this for a require() it left in the bundle. Four fixes tested on Node 24, plus the case only Cloudflare Workers hits.

    > throw Error('Dynamic require of "' + x + '" is not supported');

    FIXED3 min
  2. No.023

    Fix: TypeScript enum is not supported in strip-only mode

    Node's ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX for enum, parameter properties, namespace, export = and import =. Two ways to run it unchanged, both tested.

    > enum Color { Red, Green }

    FIXED4 min
  3. No.014

    TS1294: syntax not allowed with 'erasableSyntaxOnly', fixed

    TS1294: TypeScript rejects your enum, namespace or constructor shorthand. Why new projects turn this on, what each piece becomes, and when to switch it off.

    > src/a.ts(1,6): error TS1294: This syntax is not allowed when 'erasableSyntaxOnly' is enabled.

    FIXED4 min
  4. No.013

    Fix: ERR_MODULE_NOT_FOUND Cannot find module in ES modules

    Node can't find a file that is clearly there. What ES modules changed about relative imports, why TypeScript path aliases break at runtime, and what to change.

    > node:internal/modules/esm/resolve:275

    FIXED4 min
  5. No.012

    Unknown file extension ".ts" (ERR_UNKNOWN_FILE_EXTENSION)

    Node refuses to run your TypeScript file. The version where this stops happening, what ts-node has to do with it, and the one-line way to run .ts today.

    > node:internal/modules/esm/get_format:189

    FIXED5 min
  6. No.005

    Fix: ERR_REQUIRE_ESM require() of ES Module not supported

    require() of an ES module: which Node versions throw ERR_REQUIRE_ESM (tested 20 to 26), the .default trap after upgrading, top-level await, and fixes.

    > Error [ERR_REQUIRE_ESM]: require() of ES Module ...\node_modules\chalk\source\index.js

    FIXED4 min
  7. No.002

    Fix: require, __dirname not defined in ES module scope

    ReferenceError: require, __dirname or __filename is not defined in ES module scope. Why Node says it, and the fixes, tested on Node 24 and 26.

    > ReferenceError: __dirname is not defined in ES module scope

    FIXED4 min
  8. No.001

    Fix: “Cannot use import statement outside a module”

    Why Node, Jest, TypeScript output or your browser refuses import (or throws Unexpected token 'export'), and the fix for each. Tested on Node 20 to 26.

    > SyntaxError: Cannot use import statement outside a module

    FIXED6 min

Post card · Every Sunday

What broke this week.

One email on Sundays: the Node, TypeScript, Python and AI SDK releases that broke builds that week, each one reproduced, with a link to the fix that worked. Never shared, and you can leave any time.

— Nilay

or follow by RSSor on X

By subscribing you agree to the privacy note. One click to leave.

tip: paste the exact error text