Stop Manually Migrating Node.js to ESM: Let Claude Handle It

Written by

in

You’re stuck: your Node.js project is on CommonJS, but you need ESM to use modern packages and stay current.

The problem: manually rewriting require() to import, renaming module.exports, and handling the edge cases (__dirname doesn’t exist, circular dependencies break, some patterns don’t translate) takes days and leaves you paranoid.

The fix: use Claude Code’s batch-processing workflow to parallelize the migration.

The workflow:

  1. Run a quick scan: ask Claude Code to identify all CommonJS patterns (every require, every module.exports, __dirname, __filename, dynamic requires). It also spots circular dependencies and risky patterns.
  2. Batch-convert modules: split your src/ into files and hand them to Claude Code in parallel batches. Each batch converts its files to ESM, handling the edge cases (replaces __dirname with import.meta.url logic, wraps dynamic requires in Promise-based fallbacks).
  3. Verify in real time: after each batch, run your test suite. If a batch breaks, Claude Code shows you exactly which file caused it and fixes it before moving on.
  4. Update config: once files are converted, Claude Code updates your package.json (sets type: module), build tool config, and any explicit exports map.

The gotchas to watch:

__dirname and __filename don’t exist in ESM modules. The fix: import.meta.url becomes your reference point (one extra import per file that needs it).

Circular dependencies get exposed in ESM where they were hidden in CommonJS. Claude Code spots them; you decide if you restructure or work around.

Some packages don’t export ESM yet. For those, ask Claude Code to generate a thin CommonJS re-export wrapper so you can keep importing the old way (temporary bridge).

The whole migration runs in parallel, verified by your test suite every step. No manual find-and-replace, no guessing.

——

Book your free AI clarity call, NOW!
https://buff.ly/TpWy277

——

Repost this. Thanks.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *