npm Cheat Sheet

Here’s a cheat sheet for commonly used npm (Node Package Manager) commands:

Project Initialization

Initialize a new project:

npm init

Skip interactive setup (default values):

npm init -y

Installing Packages

Install a package locally:

npm install package-name

Install a package globally:

npm install -g package-name

Install a specific version:

npm install [email protected]

Install packages and save to dependencies (default):

npm install package-name

Install packages and save to devDependencies:

npm install package-name --save-dev

Install packages and save to peerDependencies:

npm install package-name --save-peer

Updating Packages

Update packages to their latest versions:

npm update

Update a specific package:

npm update package-name

Uninstalling Packages

Uninstall a package:

npm uninstall package-name

Uninstall a global package:

npm uninstall -g package-name

Listing Packages

List installed packages:

npm ls

List global packages:

npm ls -g --depth=0

Script Execution

Run a script defined in package.json:

npm run script-name

Publishing Packages

Publish a package:

npm publish

Searching for Packages

Search for packages:

npm search package-name

View package details:

npm show package-name

Handling Versioning

  • Semantic Versioning (SemVer) Cheat Sheet:
    • ^1.2.3: Allow patch-level changes. (1.x.x)
    • ~1.2.3: Allow only patch-level changes. (1.2.x)
    • 1.2.x: Allow only patch-level changes. (same as ~1.2.3)
    • >=1.2.3: Minimum version (1.2.3 and above)
    • <1.2.3: Maximum version (1.2.2 and below)

Here is a dedicated Semantic Versioning (SemVer) Cheat Sheet.

Miscellaneous

Check for outdated packages:

npm outdated

Audit your project for vulnerabilities:

npm audit

Fix vulnerabilities automatically:

npm audit fix

Create a .npmignore file:

  • Specify files/directories to be ignored when publishing.

Global Configuration

View npm configuration:

npm config list

Set a configuration:

npm config set key value

Help

Get help on a specific command:

npm help command

This cheat sheet covers some of the most commonly used npm commands. npm is a powerful tool with many features, so be sure to refer to the official npm documentation for more details on specific commands and advanced use cases.