Skip to main content
Utilities for removing dependencies, analyzing scripts, and transforming Node.js arguments in package.json files.

Dependency Management

removeDependencies

Removes dependencies from dependencies and devDependencies in package.json, then optionally runs the detected package manager install command.
string | string[]
required
Package name(s) to remove from both dependencies and devDependencies
object
Optional configuration object
string
default:"package.json"
Path to the target package.json file
boolean
default:"true"
Set to false to skip running package manager install after changes
boolean
default:"true"
Set to false to preview returned JSON without writing to disk
Returns: Promise<string | null>
  • Returns updated package.json content as a string when changes are applied
  • Returns null when:
    • There is nothing to remove
    • No package.json exists at the specified path
    • A parsing or runtime failure occurs

Package Manager Detection

The function automatically detects the package manager in this order:
  1. package.json packageManager field
  2. Lockfile detection:
    • pnpm-lock.yaml → pnpm
    • yarn.lock → yarn
  3. Defaults to npm
When runInstall is enabled (default), it runs <package-manager> install in the package directory.

Usage Examples

Script Analysis

getScriptsNode

Finds the “scripts” section in a package.json AST.
SgRoot
required
The root node of the package.json AST (parsed as JSON)
Returns: SgNode[] - Array of nodes representing pairs in the scripts section

getNodeJsUsage

Finds all references to node or node.exe in package.json scripts.
SgRoot
required
The root node of the package.json AST
Returns: Array<{ node: SgNode, text: () => string }> - Array of objects containing:
  • node: The AST node for the script
  • text(): Function that returns the unquoted script content

Node.js Argument Transformation

replaceNodeJsArgs

Replaces Node.js script arguments in package.json scripts. Useful for normalizing or rewriting CLI arguments used with node.
SgRoot
required
The root node of the package.json AST
Record<string, string>
required
A mapping of argument names to their replacement values. Use empty string to remove an argument.
Returns: Edit[] - Array of edit operations to apply

removeNodeJsArgs

Removes specified arguments from Node.js script usages in package.json.
SgRoot
required
The root node of the package.json AST
string[]
required
Array of argument names to remove from Node.js commands in scripts
Returns: Edit[] - Array of edit operations to apply

Complete Example: Migrating Package Scripts

Migration Patterns

Detecting Build Tool Usage

Updating Node.js Flags Across Scripts