npm common commands

May-30, 2021 · 5min

change language

Collect and organize common commands for the npm package manager, including module installation and uninstallation, configuration management, mirror source settings, version control, and other practical techniques.

View current npm configuration

npm config list

Create module

It will generate a package.json file with the information of the current project

npm init

CNPM Taobao mirror

View current mirror source

npm get registry

Set to Taobao mirror source

npm config set registry http://registry.npm.taobao.org/

Set back to the default official mirror

npm config set registry https://registry.npmjs.org/

You can also install cnpm directly

npm install -g cnpm --registry=https://registry.npm.taobao.org

Install Node module

It will create a node_modules directory

npm install <Module Name>

Global installation

npm install <Module Name> --global
# or
npm install <Modele Name> -g

Install in the current directory's node_modules, and add the module information to the package.json's dependencies (production environment dependencies)

npm install <Module Name> --save
# or
npm install <Module Name> -S

Install in the current directory's node_modules, and add the module information to the package.json's devDependencies (development environment dependencies)

npm install <Module Name> --save-dev
# or
npm install <Module Name> -D

Uninstall module

npm uninstall <Module Name>

Search module

npm search <Module Name>

Upgrade NPM

# View version
npm -v
# Upgrade
npm install npm -g

View current proxy

npm config get proxy

Set proxy

npm config set proxy=http://server:port

If the following error occurs

npm err! Error: connect ECONNREFUSED 127.0.0.1:8087

Solution (clear proxy)

npm config set proxy null

View installation information

View globally installed modules

npm list -g

Update module

npm update <Module Name>

Clear local cache

npm cache clear --force