npm Cheat Sheet

Core npm CLI, package inspection, package.json editing, and daily workflow commands.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Core npm CLI
Show npm help
npm help

# Open npm help.

Show npm version
npm --version

# Print the current npm version.

Initialize package.json
npm init

# Run the interactive package initializer.

Initialize package.json with defaults
npm init -y

# Create package.json using defaults.

Install dependencies from package-lock
npm install

# Install project dependencies.

Clean install from lockfile
npm ci

# Install exactly from package-lock.json for CI.

Run a package binary
npm exec -- <command>

# Run a binary from local dependencies or a temp package.

Run a package binary with npx
npx <command>

# Run a package executable without adding it to scripts.

## Inspect packages and metadata
List installed packages
npm ls

# Show installed dependency tree.

Show outdated packages
npm outdated

# List packages with newer available versions.

View package metadata
npm view <package>

# Show metadata from the registry.

View latest package version
npm view <package> version

# Show the current published version.

Explain why a package is installed
npm explain <package>

# Show why a package exists in the dependency tree.

Query installed packages
npm query ".workspace"

# Query dependency tree nodes using selectors.

Show funding information
npm fund

# Display funding URLs for dependencies.

Open package docs
npm docs <package>

# Open a package documentation page.

## package.json editing
Get a package.json field
npm pkg get scripts

# Read a field from package.json.

Set a package.json field
npm pkg set scripts.build="tsc -p ."

# Write a field to package.json.

Delete a package.json field
npm pkg delete scripts.test

# Remove a field from package.json.

Show package root
npm prefix

# Print the nearest package root path.

Show node_modules root
npm root

# Print the local node_modules directory.

## Useful daily workflows
Run a build script
npm run build

# Run the build script from package.json.

Run tests
npm test

# Run the test script.

Run lint
npm run lint

# Run a lint script.

Run start script
npm start

# Run the start script.

Run restart script
npm restart

# Run stop then start lifecycle commands if present.

Print script environment
npm run env

# Show environment variables available to scripts.

Recommended next

No recommendations yet.