Organize from rookie tutorial

One ,npm brief introduction

npm It's a companion NodeJS Package management tools installed together , Can solve NodeJS Many problems in code deployment , Common usage scenarios are as follows :

* Allow users to NPM The server downloads the third-party package written by others for local use .
* Allow users to NPM The server downloads and installs command-line programs written by others for local use .
* Allows users to upload their own packages or command-line programs to NPM Server for others .
Due to the new version of nodejs Already integrated npm, So before npm It's also installed . It can also be input through “npm -v”
To test for successful installation . The command is as follows , A version prompt indicates successful installation :
C:\Users\peipei3514>npm -v 5.5.1
If you install an older version npm, It's easy to get through npm Command to upgrade , The command is as follows ( Administrator rights ):
C:\WINDOWS\system32>npm install npm -g C:\Program Files\nodejs\node_gobal\npm
-> C:\Program Files\nodejs\node_gobal\node_modules\npm\bin\npm-cli.js C:\Program
Files\nodejs\node_gobal\npx -> C:\Program Files\nodejs\node_gobal\node_modules
\npm\bin\npx-cli.js + [email protected] added 476 packages in 17.895s
Two , use npm Command installation module

npm install Node.js The module syntax format is as follows :
npm install <Module Name>
The following example , We use npm Command installation common Node.js web Frame module express:
C:\WINDOWS\system32>npm install express -g + [email protected] added 48 packages
in 11.272s
After installation ,express The package is in the project directory node_modules In the catalog , So in the code, you only need to pass the require(‘express’)
Just the way , No third party package path needs to be specified .
var express = require('express');
Three , Global and local installation

npm The package installation of is divided into local installation (local), Global installation (global) two types , From the command line , The difference is just there -g nothing more , such as
npm install express # Local installation npm install express -g # Global installation
If the following error occurs :
npm err! Error: connect ECONNREFUSED
The solution is :
npm config set proxy null
Local installation

* Place the installation package in ./node_modules lower ( function npm Directory at command time ), without node_modules catalog , Will be executed in the current npm
Generate under command directory node_modules catalog .
* Through require() To bring in locally installed packages .
Global installation

* Place the installation package in /usr/local Next or you node Installation directory for .
* Can be used directly on the command line .
If you want to have both functions , You need to install it or use it in two places npm link.

Next we use the global installation express
C:\WINDOWS\system32>npm install express -g + [email protected] added 48 packages
in 11.272s
View installation information

You can use the following command to view all globally installed modules :
C:\WINDOWS\system32>npm list -g C:\Program Files\nodejs\node_gobal `-- express
@4.16.2 +-- [email protected] | +-- [email protected] | | `-- [email protected] | `--
[email protected] +-- [email protected] +-- [email protected] | +-- [email protected]
.0 | +-- [email protected] deduped | +-- [email protected] deduped
If you want to view the version number of a module , You can use the following commands :
C:\WINDOWS\system32>npm list express -g C:\Program Files\nodejs\node_gobal `--
[email protected]
Four , use package.json

package.json Under module directory , Properties used to define a package . Let's take a look express Packaged package.json file , be located
node_modules/express/package.json content :
{ "_from": "express", ... "_phantomChildren": {}, "_requested": { "type": "tag"
,"registry": true, ... "fetchSpec": "latest" }, "_requiredBy": [ "#USER" ],
"_resolved": "",
"_shasum": "e35c6dfe2d64b7dca0a5cd4f21781be3299e076c", "_spec": "express",
"_where": "C:\\WINDOWS\\system32", "author": { "name": "TJ Holowaychuk", "email"
:"[email protected]" }, "bugs": { "url":
"" }, "bundleDependencies": false,
"contributors": [ { "name": "Aaron Heckmann", "email":
"[email protected]" } ], "dependencies": { "accepts": "~1.3.4",
"array-flatten": "1.1.1", ... "type-is": "~1.6.15", "utils-merge": "1.0.1",
"vary": "~1.1.2" }, "deprecated": false, "description": "Fast, unopinionated,
minimalist web framework", "devDependencies": { "after": "0.8.2",
"connect-redis": "~2.4.1", ... "vhost": "~3.0.2" }, "engines": { "node": ">=
0.10.0" }, "files": [ "LICENSE", "", "", "index.js", "lib/"
],"homepage": "", "keywords": [ "express", ... "app", "api"
],"license": "MIT", "name": "express", "repository": { "type": "git", "url":
"git+" }, "scripts": { "lint": "eslint
.", ... "test-tap": "mocha --require test/support/env --reporter tap
--check-leaks --no-exit test/ test/acceptance/" }, "version": "4.16.2" }
Package.json Attribute description :

* name - Package name .
* version - Version number of the package .
* description - Description of the package .
* homepage - Bao's official website url .
* author - Name of the author of the package .
* contributors - Name of other contributors to the package .
* dependencies - List of dependent packages . If the dependent package is not installed ,npm The dependency package will be automatically installed in node_module Directory .
* repository - Type of place where package code is stored , It could be git or svn,git Available at Github upper .
* main - main Field specifies the main entry file for the program ,require(‘moduleName’) Will load this file . The default value of this field is under the module root directory
* keywords - keyword .
Five , Unload module

We can use the following command to uninstall Node.js modular .
npm uninstall express
After unloading , You can go /node_modules/ Check whether the package still exists in the directory , Or use the following command to view :
npm ls
Six , Update module

We can update the module with the following command :
npm update express
Seven , Search module

Use the following to search for modules :
npm search express
Eight ,NPM Common commands

* NPM There are many commands available , for example install and publish, use npm help All commands can be viewed .
* use npm help <command> Detailed help for a command can be viewed , for example npm help install.
* stay package.json Use in directory npm install . -g You can install the current command line program locally first , Available for local testing prior to release .
* use npm update <package> The current directory node_modules Update the corresponding module in the subdirectory to the latest version .
* use npm update <package> -g You can update the command line program installed globally to the latest version .
* use npm cache clear Can be emptied NPM Local cache , For those who use the same version number to publish new version code .
* use npm unpublish <package>@<version> You can unpublish a version code you have published .
Nine , Use Taobao NPM image

We all know that it is directly used in China npm The official image of is very slow , Taobao is recommended here NPM image .

TaoBao NPM The image is complete image , You can use this instead of the official version ( read-only ), Synchronization frequency is currently 10 minute Once to keep up with official services as much as possible .

You can use Taobao customized cnpm (gzip Compression support ) Command line tools instead of the default npm:
npm install -g cnpm --registry=
So you can use cnpm Command to install the module :
cnpm install [name]