Detailed explanation of common 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 project package JSON file

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.0 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

package. dependencies field of 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

package. devDependencies field of 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

package. Optional dependencies field of 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 package The dependencies field of the JSON file to see that the ^ * * in the version number has disappeared

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

Module dependencies are written to package After the JSON file, others 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, use - 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 module

Basic grammar

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

NPM dated checks whether the module is obsolete

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 guides the creation of a package in the project JSON file

The information of the installation package can be maintained to the package of the project JSON file for subsequent project development or cooperation with others, also known as package JSON is essential in a 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 manages 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.com:8080

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="http://r.cnpmjs.org"

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

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

Cache management module of cache NPM

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>]

The command is written in package In the start field of scripts in the JSON file, you can customize commands to configure a server environment and install a series of necessary programs, such as

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

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

If package If start is not set in the JSON file, node server will be started directly js

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>]

The command is written in package In the test field of scripts in the JSON file, you can customize this command to perform some operations, such as

"scripts": {
    "test": "gulp release"
},

At this time, entering npm test command in cmd is equivalent to executing gulpfile JS file to customize the release command.

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 publishing 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 sets the access level on the published package

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>]

npm package.json syntax

Original English version: https://docs.npmjs.com/files/package.json

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 the root directory of the package has server JS file, npm will set the start command to node server by default js.

  • "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 the root directory of the package has binding For the gyp file, 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.

name

In package The most important fields in 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 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. http://registry.npmjs.org/

version

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).

description

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

keywords

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

bugs

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

{
 "url" : "http://github.com/owner/project/issues", 
"email" : "project@hostname.com"
}

license

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" : "http://github.com/owner/project/path/to/license"
  }
]

repository

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" : "http://github.com/isaacs/npm.git"
  }

"repository" :
  { "type" : "svn"
  , "url" : "http://v8.googlecode.com/svn/trunk/"
  }

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

"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

"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).

dependencies

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 must be exactly the same as version
  • >Version must be larger than version
  • >=The version is the same as above
  • < version ditto
  • < = version ditto
  • ~version is about the same, see 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 as"*
  • version1 - version2 are the same as > = Version1 < = version2
  • Range1 | range2.
  • 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" : "http://asdf.com/asdf.tar.gz"
  , "til" : "~1.2"
  , "elf" : "~1.2.3"
  , "two" : "2.x"
  , "thr" : "3.3.x"
  }
}

devDependencies

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/waza.coffee"
  },
  "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.

reference material:

https://docs.npmjs.com/#cli

https://github.com/ericdum/mujiang.info/issues/6/

https://segmentfault.com/a/1190000004221514

Reprint
Author: Bai Shu
source: http://peunzhang.cnblogs.com/

Tags: node.js npm

Posted by chrisprse on Sun, 02 Jan 2022 01:56:27 +1030