2. npm usage, npm usage
NPM is a package management tool installed along with NodeJS. It can solve many problems in NodeJS code deployment. Common use cases include:
- Users are allowed to download third-party packages written by others from the NPM server for local use.
- Allows users to download and install command line programs written by others from the NPM server for local use.
- You can upload your own package or command line program to the NPM server for use by others.
The new nodejs version has integrated npm, so npm has been installed together. You can also enter"Npm-v"To test whether the installation is successful. Command:
$ npm -v2.3.0
Use the npm command to install the module
The syntax format of the Node. js module installed in npm is as follows:
$ npm install <Module Name>
For the following example, we use the npm command to install the commonly used Node. js web framework module.Express:
$ npm install express
After installation, the express package is placed in the node_modules directory under the project directory. Therefore, you only needRequire ('express ')You do not need to specify a third-party package path.
var express = require('express');
Global and local Installation
Npm package installation can be divided into local installation and global installation. from the command line, the difference is-g. For example
Npm install express # local installation npm install express-g # global installation
If the following error occurs:
npm err! Error: connect ECONNREFUSED 127.0.0.1:8087
Solution:
$ npm config set proxy null
Local Installation
- 1. Place the installation package under./node_modules (the directory where the npm command is run). If the node_modules directory does not exist, the node_modules directory will be generated under the directory where the npm command is currently executed.
- 2. You can use require () to introduce locally installed packages.
Global Installation
- 1. Put the installation package under/usr/local.
- 2. It can be used directly in the command line.
- 3. You cannot use require () to introduce locally installed packages.
Next we will use the global installation of express
$ npm install express -g
Uninstall Module
We can use the following command to uninstall the Node. js module.
$ npm uninstall express
After uninstalling the package, you can go to the/node_modules/directory to check whether the package exists, or run the following command to view the package:
$ npm ls
Update Module
We can use the following command to update the module:
$ npm update express
Search Module
Use the following to search for modules:
$ npm search express
Common NPM commands
NPM provides many commands, suchinstall
Andpublish
, Usenpm help
You can view all commands.
Usenpm help <command>
You can view the Detailed Help of a command, for examplenpm help install
.
Inpackage.json
Usenpm install . -g
You can first install the current command line program locally, which can be used for local tests before release.
Usenpm update <package>
You cannode_modules
Update the corresponding modules in the subdirectory to the latest version.
Usenpm update <package> -g
You can update the corresponding command line program installed globally to the latest version.
Usenpm cache clear
The NPM local cache can be cleared for users who release new versions of code with the same version number.
Usenpm unpublish <package>@<version>
You can cancel the release of a version code you have released.