What is Node.js Userland Migrations?
Node.js Userland Migrations is an official collection of automated code transformation tools (codemods) designed to help you maintain your Node.js codebase as the platform evolves. These codemods facilitate:- Adopting new Node.js features - Automatically update your code to use modern Node.js APIs
- Handling breaking changes - Migrate deprecated APIs to their current equivalents
- Maintaining code quality - Ensure your codebase stays up-to-date with Node.js best practices
This repository contains codemods for “userland” code - the application code you write, not Node.js core itself.
What are codemods?
Codemods are automated scripts that transform source code programmatically. Unlike simple find-and-replace operations, codemods understand the structure of your code using Abstract Syntax Trees (AST).How codemods work
Parse your code
The codemod reads your JavaScript or TypeScript files and parses them into an Abstract Syntax Tree (AST), which represents the structure of your code.
Find patterns
The codemod searches for specific patterns in the AST - for example, calls to a deprecated API like
util.log().Transform code
When a pattern is found, the codemod applies transformations - replacing old code with new, equivalent code that uses modern APIs.
AST-based transformations
Node.js Userland Migrations uses ast-grep and the jssg API to ensure accurate, safe transformations:- Identifies the
util.log()call - Replaces it with the modern equivalent
- Removes the unused import if
utilis no longer needed - Preserves your code style and formatting
Why use codemods?
Save time
Automatically transform hundreds of files in seconds instead of manually updating each one.
Reduce errors
AST-based transformations are more accurate than manual find-and-replace operations.
Handle complexity
Codemods understand different import styles, variable naming, and code patterns automatically.
Stay current
Keep your codebase updated with the latest Node.js APIs and best practices.
Available migrations
Node.js Userland Migrations includes 28 migration recipes covering:- Buffer APIs - Migrate
buffer.atob(),buffer.btoa(), andSlowBufferdeprecations - Crypto APIs - Update
crypto.fips,crypto.createCredentials(), and RSA-PSS parameters - File System - Migrate
fs.rmdir(),fs.truncate(), and access mode constants - Utilities - Replace deprecated
util.is*(),util.log(),util.print(), andutil.extend()methods - Process APIs - Update
process.mainModuleandprocess.assertusages - Timers - Migrate deprecated timer functions like
enroll(),unenroll(), andactive() - HTTP/REPL - Fix class instantiation and update deprecated APIs
- Import syntax - Convert import assertions to import attributes
- TypeScript - Correct import specifiers for Node.js ESM compliance
- And more - URL parsing, zlib, dirent, and other Node.js APIs
Real-world example
Consider migrating deprecatedutil.is*() methods across your codebase:
util.is*() calls across your entire project in seconds.
Module system support
All codemods support both CommonJS and ES modules:When to use codemods
Upgrading Node.js versions
Upgrading Node.js versions
When upgrading to a new Node.js version that deprecates APIs you’re using, codemods can automatically migrate your code to the new APIs.
Adopting new features
Adopting new features
When Node.js introduces new built-in APIs that replace third-party packages (like util.styleText replacing chalk), codemods can handle the migration.
Cleaning up deprecation warnings
Cleaning up deprecation warnings
If your application logs deprecation warnings, codemods can eliminate them by updating to the current APIs.
Large-scale refactoring
Large-scale refactoring
When you need to update patterns across dozens or hundreds of files, codemods are much faster and more reliable than manual changes.
What’s next?
Quick Start
Run your first migration in minutes
Installation
Set up the Codemod CLI and learn advanced options
Browse Recipes
Explore all available migration recipes
How Codemods Work
Deep dive into the technical details