Migration & Upgradesintermediate
Convert CommonJS to ES Modules
✓Works with OpenClaudeYou are a JavaScript migration specialist. The user wants to convert CommonJS (CJS) modules to ES Modules (ESM) syntax and structure.
What to check first
- Run
node --versionto confirm Node.js 12.20+ (required for ESM support) - Check
package.jsonand add"type": "module"to enable ESM parsing, or use.mjsfile extensions - Scan your codebase with
grep -r "require(" --include="*.js"to identify all CJS imports - Verify if you're using conditional exports or dynamic requires that need special handling
Steps
- Update
package.jsonby adding"type": "module"at the root level, or rename files from.jsto.mjs - Replace all
const x = require('module')withimport x from 'module'(named exports becomeimport { x } from 'module') - Replace
module.exports = {}withexport default {}or use named exports withexport const x = {} - Convert
require.resolve()calls toimport.meta.resolve()(Node.js 18.19+) or use a workaround withcreateRequire - Replace
__dirnameand__filenamewith equivalents usingimport.meta.urland thepathmodule orfileURLToPath - Update dynamic requires like
require(variable)toimport(variable)which returns a Promise - Handle circular dependencies by reorganizing exports or using lazy imports with
import()inside functions - Test with
node --check file.jsto validate syntax, then run your test suite to verify runtime behavior
Code
// Before: CommonJS
const path = require('path');
const { readFile } = require('fs/promises');
const myModule = require('./utils');
module.exports = {
processFile: async (filePath) => {
const content = await readFile(filePath, 'utf-8');
return myModule.transform(content);
}
};
// After: ES Modules
import path from 'path';
import { readFile } from 'fs/promises';
import { fileURLToPath } from 'url';
import * as myModule from './utils.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const processFile = async (filePath) => {
const content = await readFile(filePath, 'utf-8');
return myModule.transform(content);
};
// Handling dynamic requires
export const loadPlugin = async (pluginName) => {
const module = await import(`./plugins/${pluginName}.js`);
return module.default;
};
// Mixed export (default + named)
export default {
version: '1.0.0'
};
export const helper = () => 'helper function';
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Migration & Upgrades Skills
Other Claude Code skills in the same category — free to download.
Migration & Upgradesintermediate
Node Upgrade
Upgrade Node.js version with compatibility fixes
Migration & Upgradesintermediate
React Upgrade
Upgrade React to latest version
Migration & Upgradesadvanced
Next.js Migration
Migrate from Pages Router to App Router
Migration & Upgradesintermediate
TypeScript Migration
Migrate JavaScript project to TypeScript
Migration & Upgradesintermediate
Jest to Vitest
Migrate from Jest to Vitest
Migration & Upgradesintermediate
Webpack to Vite
Migrate from Webpack to Vite
Migration & Upgradesadvanced
Database Migration
Migrate between databases
Want a Migration & Upgrades skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.