Complete collection of npm commands


What is npm

The full name of NPM is Node Package Manager. It is a package management and distribution tool installed with NodeJS. It is convenient for JavaScript developers to download, install, upload and manage installed packages.

npm install install module

Basic grammar

npm install (with no args, in package dir)
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
npm install <tarball file>
npm install <tarball url>
npm install <folder>

alias: npm i
common options: [-S|--save|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [--dry-run]

Installation package, the latest version will be installed by default

npm install gulp

Install the specified version

npm install gulp@3.9.1

Install the package and keep the information in the package.json file of the project

The project's dependence on modules can be represented by the following three methods (assuming that the current version number is 1.1.0):

  • Patch version newly released by compatibility module: ~ 1.1.0, 1.1.x, 1.1
  • Newly released minor version and patch version of compatibility module: ^ 1.1.0, 1.x, 1
  • Newly released large version, small version and patch version of compatible module: *, x

-S. -- save installation package information will be added to the dependencies (dependencies in the production phase)

npm install gulp --save or npm install gulp -S

dependencies field of package.json file:

"dependencies": {
    "gulp": "^3.9.1"

-D. -- save dev installation package information will be added to devdependences (dependencies in the development phase), so it is generally used in the development phase

npm install gulp --save-dev or npm install gulp -D

devDependencies field of package.json file:

"devDependencies": {
    "gulp": "^3.9.1"

-O. -- save optional installation package information will be added to optional dependencies

npm install gulp --save-optional or npm install gulp -O

Optional dependencies field of package.json file:

"optionalDependencies": {
    "gulp": "^3.9.1"

-E. -- save exact precisely installs the specified module version

npm install gulp --save-exact or npm install gulp -E

Enter the command npm install gulp -ES, and pay attention to the dependencies field of the package.json file to see that the ^ in the version number disappears

"dependencies": {
    "gulp": "3.9.1"

After the module dependencies are written into the package.json file, others can open the root directory of the project (project open source, internal team cooperation), and use the npm install command to install all dependent packages according to the dependencies configuration

npm install

Local installation (local)

npm install gulp

global, using  - g or -- global

npm install gulp -g

npm uninstall   Uninstall module  

Basic grammar

npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|-D|--save-dev|-O|--save-optional]

aliases: remove, rm, r, un, unlink

For example, uninstall the module of the development version

npm uninstall gulp --save-dev

npm update   Update module

Basic grammar

npm update [-g] [<pkg>...]

npm outdated   Check whether the module is out of date

Basic grammar

npm outdated [[<@scope>/]<pkg> ...]

This command will list all outdated packages, which can be updated in time

npm ls   View installed modules

Basic grammar

npm ls [[<@scope>/]<pkg> ...]

aliases: list, la, ll

View globally installed modules and dependencies  

npm ls -g 

npm init   Create a package.json file in the project

The information of the installation package can be kept in the package.json file of the project for subsequent project development or cooperation with others. In other words, package.json is essential in the project.

npm init [-f|--force|-y|--yes]

npm help view detailed help for a command  

Basic grammar

npm help <term> [<terms..>]

For example, enter npm help install, and the system will open the file / nodejs / node of the local nodejs installation package in the default browser or default editor_ modules/npm/html/doc/cli/npm-install.html

npm help install

npm   root   View the installation path of the package

output   node_ Path to modules

npm root [-g]

npm config   Manage the configuration path of NPM

Basic grammar

npm config set <key> <value> [-g|--global]
npm config get <key>
npm config delete <key>
npm config list
npm config edit
npm get <key>
npm set <key> <value> [-g|--global]

For config, you should set up an agent to solve the problem that npm fails to install some modules

For example, I can't install any modules in the company's intranet because of the company's firewall. At this time, setting an agent can be solved

npm config set proxy=http://xxx

Another example is the domestic network environment. An official IP may be harmonious. Fortunately, there are good people in China who have built an image. At this time, we simply set up an image

npm config set registry=""

It can also be configured temporarily, such as installing Taobao image

npm install -g cnpm --registry=

npm cache management module

Basic grammar

npm cache add <tarball file>
npm cache add <folder>
npm cache add <tarball url>
npm cache add <name>@<version>

npm cache ls [<path>]

npm cache clean [<path>]

The most common command is to clear the npm local cache

npm cache clean

npm start module

Basic grammar

npm start [-- <args>]

This command is written in the start field of scripts in the package.json file. You can customize the command to configure a server environment and install a series of necessary programs, such as

"scripts": {
    "start": "gulp -ws"

At this time, entering the npm start command in cmd is equivalent to executing the watch and server commands customized by gulpfile.js file.

If the package.json file does not set start, node server.js will be started directly

npm stop module

Basic grammar

npm stop [-- <args>]

npm restart restart module

Basic grammar

npm restart [-- <args>]

npm test module

Basic grammar

npm test [-- <args>]
npm tst [-- <args>]

This command is written in the test field of scripts in the package.json file. You can customize this command to perform some operations, such as

"scripts": {
    "test": "gulp release"

At this time, entering the npm test command in cmd is equivalent to executing the release command customized by gulpfile.js file.

npm version view module version

Basic grammar

npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]

'npm [-v | --version]' to print npm version
'npm view <pkg> version' to view a package's published version
'npm ls' to inspect current package/dependency versions

View the version of the module

npm version

npm view to view the registration information of the module

Basic grammar

npm view [<@scope>/]<name>[@<version>] [<field>[.<subfield>]...]

aliases: info, show, v

View module dependencies

npm view gulp dependencies

View the source file address of the module

npm view gulp repository.url

View the contributors of the module, including the email address

npm view npm contributors

npm   adduser user login

Basic grammar

npm adduser [--registry=url] [--scope=@orgname] [--always-auth]

Before publishing a template to the npm community, you need to log in first, and then enter the publishing operation

npm publish   Release module

Basic grammar

npm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>]

Publishes '.' if no argument supplied
Sets tag 'latest' if no --tag specified

npm access   Set access levels on published packages

Basic grammar

npm access public [<package>]
npm access restricted [<package>]

npm access grant <read-only|read-write> <scope:team> [<package>]
npm access revoke <scope:team> [<package>]

npm access ls-packages [<user>|<scope>|<scope:team>]
npm access ls-collaborators [<package> [<user>]]
npm access edit [<package>]

Syntax of npm package.json

Original English version:

There are many contents in this piece, which are sorted out by well intentioned people in China:< npm package.json Chinese document >, copy some common from this document, as follows:

Default value

npm will set some default values according to the package content.

  • "scripts": {"start": "node server.js"}

    If there is a server.js file in the root directory of the package, npm will set the start command to node server.js by default.

  • "scripts":{"preinstall": "node-waf clean || true; node-waf configure build"}

    If there is a wscript file in the root directory of the package, npm will compile the preinstall command with node WAF by default.

  • "scripts":{"preinstall": "node-gyp rebuild"}

    If there is a binding.gyp file in the root directory of the package, npm will compile the preinstall command with node gyp by default.

  • "contributors": [...]

    If the root directory of the package has an AUTHORS file, npm will process it line by line in the format of name < email > (url) by default. Mailbox and url are optional# Lines beginning with a sign and a space are ignored.


The most important fields in package.json are the name and version fields. They are all necessary. If they are not available, they cannot be install ed. The identifier composed of name and version is unique in the assumption. The change package should also change the version.

Name is the name of this thing. be careful:

  • Don't put node or js in the name. Because you wrote package.json, it is assumed to be js, but you can specify an engine with the "engine" field (see later).
  • This name will be used as part of the URL, a command line parameter, or the name of the folder. Any non URL safe character cannot be used.
  • This name may be passed as a parameter to require(), so it should be short but clear.
  • Before you fall in love with your name, you may want to go to npm registry to check whether the name has been used.


version must be able to be node-semver Resolution, which is wrapped in npm dependencies. (if you want to use it yourself, you can execute npm install semver)

Available "numbers" or "ranges" are shown in semver(7).


Put the introduction and string to facilitate the search in npm search


Keyword, array, string, convenient for searching in npm search


The url and / or email address of the submission question for your project

 "url" : "", 
"email" : ""


You should specify a license to let people know the rights and restrictions of use.

The simplest way is that if you use a general license like BSD or MIT, you only need to specify the name of a license, like this:

{ "license" : "BSD" }

If you have more complex license conditions or want to provide more details, you can do this:

"licenses" : [
  { "type" : "MyLicense"
  , "url" : ""


Specify where your code is stored. This is helpful to those who want to contribute. If the git repository is on the github, the npm docs command can find you.

Do it in this way.

"repository" :
  { "type" : "git"
  , "url" : ""

"repository" :
  { "type" : "svn"
  , "url" : ""

URLs should be public (even read-only) URLs that can be processed directly by unmodified version control programs. It should not be an html project page. Because it's for computers.


"scripts" is a hash object composed of script commands, which are executed in different life cycles of the package. key is the lifecycle event and value is the command to run.

Refer to above npm start,npm test command

For more details, see   npm-scripts(7)


"config" hash can be used to configure Cross Version parameters used in package scripts. In the instance, if a package has the following configuration:

 "name" : "foo",
 "config" : { "port" : "8080" } 

Then there is a "start" command that references npm_package_config_port environment variable. Users can override it through npm config set foo:port 8001.

See   npm-config(7)   and   npm-scripts(7).


A dependency is a hash that assigns a version range to a set of package names. This version range is a string separated by one or more spaces. Dependencies can also use tarball or git URL s.

Do not place tests or transitional dependencies in dependencies hash. See dev dependencies below

See details semver(7).

  • version   It must be completely consistent with version
  • >version   Must be greater than version
  • >=version   ditto
  • <version   ditto
  • <=version   ditto
  • ~version   About the same, see you semver(7)
  • 1.2.x   1.2.0, 1.2.1, etc., but excluding 1.3.0
  • http://...   See 'dependent URL' below
  • *   All
  • ""   Empty, same*
  • version1 - version2   with  >= version1 <=version2.
  • range1 || range2   either-or.
  • git...   See 'rely on Git URL' below
  • user/repo   See 'GitHub URLs' below

For example, the following are legal:

{ "dependencies" :
  { "foo" : "1.0.0 - 2.9999.9999"
  , "bar" : ">=1.0.2 <2.1.2"
  , "baz" : ">1.0.2 <=2.3.4"
  , "boo" : "2.0.1"
  , "qux" : "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0"
  , "asd" : ""
  , "til" : "~1.2"
  , "elf" : "~1.2.3"
  , "two" : "2.x"
  , "thr" : "3.3.x"


If someone wants to use your module, they may not need the external test or documentation framework you develop and use.

In this case, it is best to list these affiliated items in dev dependencies.

These things will be initialized when npm link or npm install is executed, and can be managed like other npm configuration parameters. See details npm-config(7).

For the construction steps of non-specific platform, such as the need to compile CoffeeScript, you can use the prepublish script to implement it, and put the package it depends on in devDependency. (Note: prepublish defines the script to be executed first when npm publish is executed)

For example:

{ "name": "ethopia-waza",
  "description": "a delightfully fruity coffee varietal",
  "version": "1.2.3",
  "devDependencies": {
    "coffee-script": "~1.6.3"
  "scripts": {
    "prepublish": "coffee -o lib/ -c src/"
  "main": "lib/waza.js"

The pre publish script will be run before publishing, so that users can use it without compiling it by themselves. And in development mode (such as running npm install locally), this script will be run for better testing.


Refer to other materials:


Tags: node.js

Posted on Sun, 28 Nov 2021 20:54:57 -0500 by