Node.js NPM Common Commands Daquan _node.js

Source: Internet
Author: User
Tags aliases node server

What's NPM?

NPM's full name is Node Package Manager, the package management and distribution tool that is installed along with Nodejs, which makes it easy for JavaScript developers to download, install, upload, and manage installed packages.

NPM Install installation Module

Basic syntax

NPM install (with no args, 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]

Install the package, and the latest version will be installed by default

Copy Code code as follows:
NPM Install Gulp

Install the specified version

Copy Code code as follows:
NPM Install gulp@3.9.1

Install the package and keep the information in the project's Package.json file

Project dependencies on modules can be represented using the following 3 methods (assuming the current version number is 1.1.0):

Newly released patch version of the Compatibility module: ~1.1.0, 1.1.x, 1.1 compliant modules new releases, patch versions: ^1.1.0, 1.x, 1 compatible modules new releases, iterations, patch versions: *, X

-S,--save installation package information will be added to dependencies (production phase dependent)

Copy Code code as follows:
NPM Install Gulp--save or NPM install Gulp-s

Dependencies fields for Package.json files:

Copy Code code as follows:
"Dependencies": {"Gulp": "^3.9.1"}

-D,--save-dev installation package information will be added to the Devdependencies (dependency on the development phase), so the development phase typically uses it

Copy Code code as follows:
NPM Install Gulp--save-dev or NPM install gulp-d

Devdependencies fields for Package.json files:

Copy Code code as follows:
"Devdependencies": {"Gulp": "^3.9.1"}

-O,--save-optional installation package information will be added to optionaldependencies (optional phase dependencies)

Copy Code code as follows:
NPM Install Gulp--save-optional or NPM install Gulp-o

Optionaldependencies fields for Package.json files:

Copy Code code as follows:
"Optionaldependencies": {"Gulp": "^3.9.1"},

-E,--save-exact exact installation of the specified module version

Copy Code code as follows:
NPM Install Gulp--save-exact or NPM install GULP-E

Enter the command NPM install Gulp-es, pay attention to the dependencies field of the Package.json file to see that the ^ in the version number is gone.

Copy Code code as follows:
"Dependencies": {"Gulp": "3.9.1"}

After the module's dependencies are written to the Package.json file, others open the root directory of the project (project open source, internal team collaboration), and use the NPM Install command to install all the dependent packages according to the dependencies configuration

Copy Code code as follows:
NPM Install

Locally installed (local)

Copy Code code as follows:
NPM Install Gulp

Global installation, using-G or--global

Copy Code code as follows:
NPM Install Gulp-g

NPM LS View the installed modules

Basic syntax

Copy Code code as follows:
NPM ls [[< @scope >/]<pkg> ...] Aliases:list, LA, LL

View the globally installed modules and dependencies

Copy Code code as follows:
NPM ls-g

NPM Uninstall Uninstall module

Basic syntax

Copy Code code as follows:
NPM uninstall [< @scope >/]<pkg>[@<version>] [-s|--save|-d|--save-dev|-o|--save-optional]aliases:remove, RM, R, UN, unlink

such as uninstalling the development version of the module

Copy Code code as follows:
NPM Uninstall Gulp--save-dev

NPM Update Module

Basic syntax

Copy Code code as follows:
NPM Update [g] [<pkg> ...]

NPM outdated check if the module is obsolete

Basic syntax

Copy Code code as follows:
NPM outdated [[< @scope >/]<pkg> ...]

This command lists all outdated packages that can be updated in a timely manner

NPM Init directs the creation of a Package.json file in the project

The installation package information can be maintained in the project's Package.json file for subsequent project development or collaboration with other people, and that Package.json is essential in the project.

Copy Code code as follows:
NPM Init [-f|--force|-y|--yes]

NPM Help view the details of a command

such as inputnpm help install,系统在默认的浏览器或者默认的编辑器中打开本地nodejs安装包的文件/nodejs/node_modules/npm/html/doc/cli/npm-install.html

Copy Code code as follows:
NPM Help <command>

Npmroot View the installation path of the package

Path to Output node_modules

Copy Code code as follows:
NPM Root [-g]

NPM Config manages NPM's configuration path

Basic syntax

Copy Code code as follows:
NPM config set <key> <value> [-g|--global]npm config get <key>npm config Delete <key>npm config li STNPM config editnpm get <key>npm set <key> <value> [-g|--global]

For config this block should be used to set up the agent to solve the NPM installation of some modules failed

For example, I am in the company intranet, because the company's firewall reason, can not complete any module installation, this time the setup agent can solve

Copy Code code as follows:
NPM Config set proxy=http://dev-proxy.oa.com:8080

Also such as the domestic network environment problem, an official IP may be harmonious, fortunately, there are kind-hearted people in the country, built a mirror, at this time we simply set the mirror

Copy Code code as follows:
NPM config set registry= "http://r.cnpmjs.org"

Cache of NPM Cache management module

Basic syntax

Copy Code code as follows:
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 commands are to clear the NPM local cache

Copy Code code as follows:
NPM Cache Clean

NPM Start Start module

Basic syntax

Copy Code code as follows:
NPM start [--<args>]

This command, written in the Start field of the Package.json file scripts, allows you to customize the command to configure a server environment and install a series of necessary programs, such as

Copy Code code as follows:
"Scripts": {"Start": "Gulp-all"}

If the Package.json file does not have the start set, the node server.js is started directly

NPM Stop Stop Module

Basic syntax

Copy Code code as follows:
NPM Stop [--<args>]

NPM Restart reboot Module

Basic syntax

Copy Code code as follows:
NPM restart [--<args>]

Rush in the college entrance examination and the Dragon Boat Festival before an article, I wish the students of the college entrance exam extraordinary play, but also wish everyone happy Zongzi Festival, a lot of accompany the family.

The above is a small series to introduce the Node.js in NPM commonly used in the order of the relevant information, I hope to help you!

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.