Skip to main content

Codemod CLI

Node.js Userland Migrations uses the Codemod CLI to run migration recipes. You don’t need to install anything globally - the CLI can be run directly with npx. The simplest way to run codemods is using npx, which downloads and executes the CLI automatically:
This approach:
  • Requires no installation
  • Always uses the latest version of the CLI
  • Works in CI/CD environments
  • Doesn’t pollute your global npm packages
The first run may take a few seconds while npx downloads the Codemod CLI. Subsequent runs are faster.

Global installation (optional)

If you run codemods frequently, you can install the CLI globally:
Then run codemods without npx:

Verifying installation

Check that the Codemod CLI is available:
You should see the version number:

Prerequisites

Node.js version

Node.js Userland Migrations requires:
  • Node.js 18.0.0 or higher (recommended: latest LTS version)
  • npm 8.0.0 or higher
Check your versions:

Git

While not strictly required, git is strongly recommended:
Git allows you to:
  • Review changes with git diff
  • Revert migrations with git reset
  • Track migration history in your repository
Always ensure you have a clean git state (all changes committed) before running codemods.

Project setup

Running in an existing project

To run codemods in your project:
1

Navigate to your project directory

2

Ensure a clean git state

If you have uncommitted changes, commit them:
3

Run the codemod

The CLI will scan your project and apply transformations to JavaScript and TypeScript files.

File types supported

Codemods automatically process these file types:
  • **/*.js - JavaScript files
  • **/*.jsx - React JSX files
  • **/*.ts - TypeScript files
  • **/*.tsx - TypeScript React files
  • **/*.mjs - ES module files
  • **/*.cjs - CommonJS module files
  • **/*.mts - TypeScript ES module files
  • **/*.cts - TypeScript CommonJS files

Files excluded by default

The following directories are automatically excluded:
  • **/node_modules/** - Dependencies
  • **/dist/** - Build output
  • **/build/** - Build output
  • **/.git/** - Git repository data
  • **/coverage/** - Test coverage reports

Configuration

Codemod CLI configuration

You can configure the Codemod CLI behavior with a .codemodrc.json file in your project root:

Command-line options

Override configuration with command-line flags:

Development setup

If you want to contribute to Node.js Userland Migrations or run codemods from source:
1

Clone the repository

2

Install dependencies

This installs dependencies for all recipes (the project uses npm workspaces).
3

Run tests

This runs tests for all recipes.
4

Run a codemod from source

Navigate to your project and run a workflow file:

Project structure

The repository is organized as follows:

Running recipe tests

Test a specific recipe:

CI/CD integration

Running in GitHub Actions

You can run codemods in CI to validate migrations or apply them automatically:
.github/workflows/migrate.yml
Always test migrations locally before running them in CI. Use --dry-run mode first to verify expected changes.

Running in other CI systems

The same approach works in any CI system that supports Node.js:

Troubleshooting

If you get a “command not found” error:
  • Ensure you’re using npx codemod not just codemod
  • If using global installation, reinstall: npm install -g codemod
  • Check your PATH includes npm global binaries: npm config get prefix
On Unix systems, you may need to adjust permissions:
For large codebases, you may need to increase Node.js memory:
The first npx run downloads the CLI. To speed up subsequent runs:
  • Install globally: npm install -g codemod
  • Or use npm cache: npx will cache the package after first use

Useful resources

Codemod CLI Reference

Complete CLI documentation and command reference

Workflow Documentation

Learn about workflow files and multi-step migrations

Codemod Studio

Interactive environment for developing codemods

jssg API Reference

JavaScript AST manipulation API documentation

What’s next?

Quick Start

Run your first migration

Browse Recipes

Explore all available migration recipes

How Codemods Work

Understand the technical details

Contributing

Create your own migration recipes