Organize from rookie tutorial

One,npm brief introduction

npm Be accompanied byNodeJS Package management tools installed together, Can solveNodeJS Many problems in code deployment, Common usage scenarios are as follows:

* Allow users toNPM The server downloads the third-party package written by others for local use.
* Allow users toNPM 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 toNPM Server for others.
Due to the new version ofnodejs Already integratednpm, So beforenpm It's also installed. It can also be input through “npm -v”
To test for successful installation. The order 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 order 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>
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 Directory, 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 127.0.0.1:8087
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.
* Can pass 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 Bag package.json file, Be located
node_modules/express/package.json content:
{ "_from": "express", ... "_phantomChildren": {}, "_requested": { "type": "tag"
,"registry": true, ... "fetchSpec": "latest" }, "_requiredBy": [ "#USER" ],
"_resolved": "https://registry.npmjs.org/express/-/express-4.16.2.tgz",
"_shasum": "e35c6dfe2d64b7dca0a5cd4f21781be3299e076c", "_spec": "express",
"_where": "C:\\WINDOWS\\system32", "author": { "name": "TJ Holowaychuk", "email"
:"[email protected]" }, "bugs": { "url":
"https://github.com/expressjs/express/issues" }, "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", "History.md", "Readme.md", "index.js", "lib/"
],"homepage": "http://expressjs.com/", "keywords": [ "express", ... "app", "api"
],"license": "MIT", "name": "express", "repository": { "type": "git", "url":
"git+https://github.com/expressjs/express.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 specification:

* name - Package name.
* version - Version number of the package.
* description - Package description.
* homepage - Official website of the package 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, Could be git or svn,git Available in 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
index.js.
* keywords - Keyword.
Five, Unloading module

We can use the following command to uninstall Node.js Modular.
npm uninstall express
After unloading, You can go to /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 Frequently used commands

* NPM There are many commands available, for exampleinstall andpublish, Usenpm help All commands can be viewed.
* Usenpm help <command> Detailed help for a command can be viewed, for examplenpm help install.
* staypackage.json Use in directorynpm install . -g You can install the current command line program locally first, Available for local testing prior to release.
* Usenpm update <package> The current directorynode_modules Update the corresponding module in the subdirectory to the latest version.
* Usenpm update <package> -g You can update the command line program installed globally to the latest version.
* Usenpm cache clear Clear upNPM Local cache, For those who use the same version number to publish new version code.
* Usenpm 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 npmjs.org 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=https://registry.npm.taobao.org
So you can use cnpm Command to install the module:
cnpm install [name]