Common npm Commands
# Common npm Commands
# Introduction
npm is the package (module) manager that comes bundled with Node.js. Common use cases include:
- Allowing users to download third-party packages written by others from the npm server for local use.
- Allowing users to download and install command-line programs written by others from the npm server for local use.
- Allowing users to upload their own packages or command-line programs to the npm server for others to use.
# Common Commands
# Check Installation and Version
npm -v # Displays the version number, confirming installation
1
# Generate package.json File
npm init
1
package.json describes the modules and other information used in the project
# Install Modules
npm install # Install modules defined in package.json, shorthand: npm i
# Install a specific module
npm i <ModuleName>
# Global installation
npm i <ModuleName> -g
# Install and save to dependencies in package.json
npm i <ModuleName> --save
# Install and save to devDependencies in package.json
npm i <ModuleName> --save-dev
# Install multiple modules
npm i <ModuleName1> <ModuleName2>
# Installation flags:
-save # Shorthand -S, adds to production dependencies
-save-dev # Shorthand -D, adds to development dependencies
-g # Global install, places the package in /usr/local or your node installation directory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# View
# View all globally installed packages
npm ls -g
# View locally installed packages in the project
npm ls
# View a package's package.json
npm view <ModuleName>
# View a package's dependencies
npm view <ModuleName> dependencies
# View a package's source file URL
npm view <ModuleName> repository.url
# View the required Node.js version for a package
npm view <ModuleName> engines
# View help
npm help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Update Modules
# Update a local module
npm update <ModuleName>
# Update a global module
npm update -g <ModuleName> # Update a global package.
npm update -g # Update all global packages.
npm outdated -g --depth=0 # Find packages that need updating.
1
2
3
4
5
6
7
2
3
4
5
6
7
# Uninstall Modules
# Uninstall a local module
npm uninstall <ModuleName>
# Uninstall a global module
npm uninstall -g <ModuleName> # Uninstall a global package.
1
2
3
4
5
2
3
4
5
# Clear Cache
# Clear npm cache
npm cache clear
1
2
2
# Use Taobao Mirror
# Use Taobao mirror
npm install -g cnpm --registry=https://registry.npm.taobao.org
1
2
2
# Other
# Rebuild after changing package contents
npm rebuild <ModuleName>
# Check if packages are outdated; lists all outdated packages for timely updates
npm outdated
# Access npm's JSON file; opens a web page
npm help json
# Check if a package name already exists before publishing
npm search <ModuleName>
# Unpublish a specific version of your package
npm unpublish <package> <version>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# Tips
# Clear Cache After Multiple Failed Installations
npm cache clean -f
1
# Check Installed Package Version
npm ls <ModuleName>
1
Note: This method gives you the exact version number used in the project. When checking package.json, the "^" symbol means greater than this version.
# npm Package Publishing Tutorial
npm Package Publishing Tutorial (opens new window)
# nrm: Purpose and Usage
# What is nrm?
nrm (npm registry manager) is a mirror source management tool for npm. When overseas resources are too slow, you can use it to quickly switch between npm sources.
# Installing nrm
npm install -g nrm
1
# nrm Commands
nrm ls # View available sources (the one with * is the current source; <registry> represents the source name below)
nrm use <registry> # Switch npm download source to the specified one
nrm add <registry> <url> # Add a source, where url is the source path
nrm del <registry> # Delete a source
nrm test <registry> # Test a source's response time, useful for choosing which source to use
nrm help # View nrm help
nrm home <registry> # Open the specified source's official website
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# Using nrm
When your network isn't ideal or you're subject to network restrictions that prevent using npm's original source, nrm is very useful. Simply:
nrm ls # View available sources
nrm use <registry> # Switch to the specified source
1
2
2
Edit (opens new window)
Last Updated: 2026/03/21, 12:14:36