Introduction to NPM Usage
NPM is a package management tool that is installed with Nodejs to address many of the issues in Nodejs code deployment, with the following common usage scenarios:
- Allows users 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.
- Allows users to upload their own packages or command-line programs to the NPM server for others to use.
Since NPM has been integrated with the new version of Nodejs, NPM has been installed as well. You can also test for a successful installation by entering "Npm-v" . The following command appears and the version prompt indicates that the installation was successful:
$ npm-v2.3.0
If you are installing an older version of NPM, you can easily upgrade with the NPM command as follows:
$ sudo npm install npm-g/usr/local/bin/npm-/usr/local/lib/node_modules/npm/bin/npm-cli.js[email protected]/usr/ local/lib/node_modules/npm
If the Window system uses the following command:
NPM Install Npm-g
Installing the module using the NPM command
NPM installs the node. JS module syntax in the following format:
$ NPM Install <module name>
In the following example, we use the NPM command to install the Common node. JS Web Framework Module Express:
$ NPM Install Express
Once installed, the Express package is placed in the Node_modules directory under the project directory, so you only need to pass require (' Express ') in the code, without specifying a third-party package path.
var express = require (' Express ');
Global installation and Local installation
NPM package installation is divided into local installation (locally), the global installation of two, from the command line to see, the difference is only G, such as
NPM Install Express # Local installation npm install EXPRESS-G # Global Installation
If the following error occurs:
The solution is:
$ NPM config set proxy null
Local Installation
- 1. Place the installation package under./node_modules (the directory where the NPM command is running), and if there is no Node_modules directory, the Node_modules directory is generated under the directory where the NPM command is currently executing.
- 2. A locally installed package can be introduced via require ().
Global Installation
- 1. Place the installation package under/usr/local or your node's installation directory.
- 2. Can be used directly on the command line.
If you want to have both features, you'll need to install it in two places or use NPM link.
Next we install the express using the global approach
$ NPM Install Express-g
The installation process outputs the following, the first line outputs the version number of the module and the installation location.
[Email protected] node_modules/express├──[email protected]├──[email protected]├──[email Protected]├──[email protected]├──[email protected]├──[email protected]├──[email protected] ├──[email protected]├──[email protected]├──[email protected]├──[email protected]├──[email protected]├──[email protected]├──[email protected]├──[email protected]├──[email Protected]├──[email protected]├──[email protected]├──[email protected] ([email protected]) ├──[email protected] ([email protected]) ├──[email protected] ([email protected], [email Protected]) ├──[email protected] ([email protected]) ├──[email protected] ([email protected], [ Email protected]) ├──[email protected] ([email protected], [email protected]) └──[email Protected] ([email protected], [email protected], [email protected], [email p rotected], [email protected])
You can use the following command to view all globally installed modules:
$ NPM Ls-g
NPM Common Commands
NPM provides a number of commands, such as install
and publish
, to use npm help
to view all commands.
Use npm help <command>
the detailed help that can be used to view a command, for example npm help install
.
package.json
use in your directory to npm install . -g
install the current command-line program locally, which can be used for local testing before publishing.
npm update <package>
You can node_modules
update the corresponding module in the subdirectory of the current directory to the latest version.
Use the npm update <package> -g
corresponding command-line program to update the global installation to the latest version.
Use npm cache clear
the ability to empty the NPM local cache against people who publish new versions of code with the same version number.
Use npm unpublish <package>@<version>
to revoke a release code that you have published yourself.
Introduction to NPM Usage