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.

  1. No.028

    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.025

    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 }

    FIXED3 min
  3. No.015

    Fix: This syntax is not allowed when 'erasableSyntaxOnly' is enabled

    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.

    FIXED3 min
  4. No.014

    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

    FIXED2 min
  5. No.013

    Fix: 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

    FIXED3 min
  6. No.005

    Fix: ERR_REQUIRE_ESM require() of ES Module not supported

    require() of an ES module fails, and on newer Node a different error takes its place. Three fixes, and the trap waiting after you upgrade.

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

    FIXED1 min
  7. No.002

    Fix: __dirname is not defined in ES module scope (Node 24)

    Node refuses __dirname in ES modules. The one-line modern fix, the fallback for older Node, and the rename that makes it worse. All run on Node 24.

    > console.log(path.join(__dirname, "data.json"));

    FIXED1 min
  8. No.001

    Fix: “Cannot use import statement outside a module”

    Why Node or your browser refuses import, what changed in Node 24, and the fix for each case. Reproduced in both Node and Chrome.

    > SyntaxError: Cannot use import statement outside a module

    FIXED2 min

Post card · Newsletter

Get the next fix in your inbox.

One short email when a new entry is published. No spam, 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