Skip to main content
The @nodejs/codemod-utils package provides a comprehensive set of utilities for building Node.js codemods with ast-grep. These utilities handle complex patterns for import/require analysis, binding resolution, code transformation, and package.json processing.

Why Use These Utilities?

Building Node.js codemods requires handling complex patterns for:
  • Import/Require Analysis: Finding and analyzing import statements and require() calls for specific modules
  • Binding Resolution: Determining how imported functions are accessed locally (destructured, aliased, etc.)
  • Code Transformation: Safely removing unused imports and modifying code while preserving formatting
  • Package.json Processing: Analyzing and transforming package.json scripts that use Node.js
These utilities provide battle-tested solutions for common codemod operations, reducing boilerplate and ensuring consistent behavior across all migration recipes.

API Categories

Import and Require Detection

Utilities for finding and analyzing module dependencies:

Binding Resolution and Transformation

Utilities for resolving and manipulating import bindings:

AST-grep Helper Functions

Core utilities for working with AST nodes:

Package.json Utilities

Utilities for processing package.json files:

Type Definitions

All utilities use types from @codemod.com/jssg-types:
  • SgRoot<Js> - Root AST node for JavaScript/TypeScript
  • SgNode<Js> - Individual AST node
  • Edit - Represents a code edit operation
  • Range - Represents a position range in source code

Getting Started

Import utilities directly from their respective modules:

Common Patterns

Finding Module Dependencies

Resolving Bindings

Removing Bindings