Skip to main content
Codemods modify your source code automatically. Following these best practices ensures safe, reliable transformations.
CRITICAL: Always commit your changes before running codemods. Codemods change source code. Uncommitted changes may be lost or corrupted. This is not optional.

Pre-Flight Checklist

Before running any codemod:
1

Commit all changes

Ensure your working directory is clean with git status
2

Create a dedicated branch

Make changes on a separate branch for easy rollback
3

Backup important work

For critical migrations, create a backup branch
4

Review the codemod documentation

Understand what the codemod will change

Running Codemods Safely

Start with a Dry Run

Many codemods support dry-run mode to preview changes:
Not all codemods support --dry-run. Check the codemod’s documentation or try running with --help.

Test on a Small Scope First

Before running on your entire codebase:

Review Changes Immediately

After running a codemod:
Use a visual diff tool like git difftool or your IDE’s built-in diff viewer for easier review.

Validation Strategy

1. Run Your Test Suite

Your existing tests are the first line of defense:
What to check:
  • All tests pass
  • No new warnings or errors
  • Test coverage hasn’t decreased
  • Performance hasn’t degraded

2. Check Linting and Type Checking

Ensure code quality standards are maintained:
Common issues after codemods:
  • Unused imports (codemod removed usage but not import)
  • Type errors (API signature changes)
  • Formatting issues (indentation, spacing)
Many codemods try to preserve formatting, but some manual cleanup may be needed. Run your formatter:

3. Manual Code Review

Spot-check transformed code: Focus areas:
  • Edge cases: Look for unusual patterns the codemod might mishandle
  • Complex expressions: Nested calls, ternaries, template literals
  • Comments: Ensure comments still make sense after transformation
  • Formatting: Check indentation and readability
Example review:

4. Integration Testing

If you have integration or e2e tests:
Integration tests catch issues that unit tests miss:
  • API contract changes
  • Behavior differences in edge cases
  • Performance regressions

5. Build and Package

Ensure the project still builds:

Rollback Strategies

Quick Rollback with Git

If something goes wrong:
git reset --hard permanently deletes uncommitted changes. Use with caution.

Selective Rollback

Rollback specific files:

Revert Merged Changes

If the codemod was already merged:

Advanced Safety Techniques

Incremental Migration

For large codebases, migrate incrementally:
1

Phase 1: Core utilities

Run codemod on your utility library first
2

Phase 2: Individual modules

Migrate one module at a time
3

Phase 3: Test and iterate

Run tests after each phase, fix issues before proceeding
4

Phase 4: Remaining code

Complete migration across the codebase
Benefits:
  • Easier to isolate issues
  • Smaller, reviewable commits
  • Less risky than “big bang” migration
  • Can pause and resume migration

Automated Safety Checks

Add pre-commit hooks to catch issues:
Or use tools like husky:

Snapshot Testing

For critical code, create snapshots before migration:

Testing Codemods Locally

If you’re developing or testing a codemod:

From Source

Test Fixtures

Create test cases before running on real code:
Run the codemod on the fixture and verify results:

Common Issues and Solutions

Issue: Codemod Skipped Files

Symptom: Some files weren’t transformed Cause: The codemod didn’t find matching patterns Solution:

Issue: Syntax Errors After Codemod

Symptom: Code doesn’t parse or run Cause: Codemod generated invalid syntax Solution:

Issue: Tests Failing

Symptom: Previously passing tests now fail Cause: Behavior change or edge case not handled Solution:
  1. Review the failing test
  2. Check if the transformation was correct
  3. Update test if the change is intentional
  4. Fix the code if the transformation was wrong

Issue: Merge Conflicts

Symptom: Git conflicts when merging migration branch Cause: Main branch changed during migration Solution:

Team Coordination

When running codemods on shared codebases:

Communication

  1. Announce the migration in your team channel
  2. Freeze PRs temporarily to avoid conflicts
  3. Schedule migration during low-activity periods
  4. Document the process for team reference

Pull Request Best Practices

Good PR structure:
Commit strategy:

Code Review Tips

For reviewers:
  • Trust but verify: Codemods are reliable but not perfect
  • Spot-check edge cases: Don’t review every line, focus on complex patterns
  • Run tests locally: Don’t rely solely on CI
  • Check the diff stats: Unusual patterns might indicate issues

Production Deployment

Before deploying codemod changes to production:
1

Staging environment

Deploy to staging first
2

Smoke tests

Run critical path tests in staging
3

Monitor metrics

Check performance, error rates, logs
4

Gradual rollout

Use feature flags or canary deployments if possible
5

Production deployment

Deploy to production with rollback plan ready
For critical production systems, consider deploying migrations during maintenance windows or low-traffic periods.

Next Steps

How Codemods Work

Understand the technical foundation of AST transformations

When to Use Codemods

Learn when codemods are the right choice

Additional Resources

The Node.js Userland codemods are battle-tested and used across thousands of projects. Most issues you encounter will be edge cases specific to your codebase, not bugs in the codemod itself.