2. npm usage, npm usage

Source: Internet
Author: User

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, suchinstallAndpublish, Usenpm helpYou can view all commands.

  • Usenpm help <command>You can view the Detailed Help of a command, for examplenpm help install.

  • Inpackage.jsonUsenpm install . -gYou can first install the current command line program locally, which can be used for local tests before release.

  • Usenpm update <package>You cannode_modulesUpdate the corresponding modules in the subdirectory to the latest version.

  • Usenpm update <package> -gYou can update the corresponding command line program installed globally to the latest version.

  • Usenpm cache clearThe 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.

 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.