Introduction to using NPM
NPM is a package management tool installed along with NodeJS. It can solve many problems in NodeJS code deployment. Common usage scenarios include the following:
Allow users Download third-party packages written by others from the NPM server and use them locally.
Allows users to download and install command line programs written by others from the NPM server for local use.
Allows users to upload packages or command line programs they write to the NPM server for others to use.
Since the new version of nodejs has integrated npm, npm has also been installed before. You can also test whether the installation is successful by entering "npm -v" . The command is as follows. If the version prompt appears, it means the installation is successful:
$ npm -v 2.3.0
If you are installing an old version of npm, you can easily upgrade it through the npm command. The command is as follows:
$ sudo npm install npm -g /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js npm@2.14.2 /usr/local/lib/node_modules/npm
If it is Window The system can use the following command:
npm install npm -g
Use npm command to install the module
npm installs Node.js module syntax format is as follows:
$ npm install <Module Name>
In the following example, we use The npm command installs 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, so in the code only Just use require('express'). There is no need to specify the third-party package path.
var express = require('express');
Global installation and local installation
npm package installation is divided into two types: local installation (local) and global installation (global). Judging from the command line, the difference Just without -g, for example
npm install express # 本地安装 npm install express -g # 全局安装
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
1. Place the installation package under ./node_modules (the directory where the npm command is run). If there is no node_modules directory, the node_modules directory will be generated in the directory where the npm command is currently executed.
2. Locally installed packages can be introduced through require().
Global installation
1. Place the installation package under /usr/local or the installation directory of your node.
2. Can be used directly in the command line.
If you want to have the functionality of both, you need to install it in two places or use npm link.
Next we use the global method to install express
$ npm install express -g
The installation process outputs the following content. The first line outputs the module version number and installation location.
express@4.13.3 node_modules/express ├── escape-html@1.0.2 ├── range-parser@1.0.2 ├── merge-descriptors@1.0.0 ├── array-flatten@1.1.1 ├── cookie@0.1.3 ├── utils-merge@1.0.0 ├── parseurl@1.3.0 ├── cookie-signature@1.0.6 ├── methods@1.1.1 ├── fresh@0.3.0 ├── vary@1.0.1 ├── path-to-regexp@0.1.7 ├── content-type@1.0.1 ├── etag@1.7.0 ├── serve-static@1.10.0 ├── content-disposition@0.5.0 ├── depd@1.0.1 ├── qs@4.0.0 ├── finalhandler@0.4.0 (unpipe@1.0.0) ├── on-finished@2.3.0 (ee-first@1.1.1) ├── proxy-addr@1.0.8 (forwarded@0.1.0, ipaddr.js@1.0.1) ├── debug@2.2.0 (ms@0.7.1) ├── type-is@1.6.8 (media-typer@0.3.0, mime-types@2.1.6) ├── accepts@1.2.12 (negotiator@0.5.3, mime-types@2.1.6) └── send@0.13.0 (destroy@1.0.3, statuses@1.2.1, ms@0.7.1, mime@1.3.4, http-errors@1.3.1)
You can use the following command to view all globally installed modules:
$ npm ls -g
Use package.json
package.json is located in the directory of the module, use Used to define package properties. Next, let’s take a look at the package.json file of the express package, located at node_modules/express/package.json. Content:
{ "name": "express", "description": "Fast, unopinionated, minimalist web framework", "version": "4.13.3", "author": { "name": "TJ Holowaychuk", "email": "tj@vision-media.ca" }, "contributors": [ { "name": "Aaron Heckmann", "email": "aaron.heckmann+github@gmail.com" }, { "name": "Ciaran Jessup", "email": "ciaranj@gmail.com" }, { "name": "Douglas Christopher Wilson", "email": "doug@somethingdoug.com" }, { "name": "Guillermo Rauch", "email": "rauchg@gmail.com" }, { "name": "Jonathan Ong", "email": "me@jongleberry.com" }, { "name": "Roman Shtylman", "email": "shtylman+expressjs@gmail.com" }, { "name": "Young Jae Sim", "email": "hanul@hanul.me" } ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/strongloop/express.git" }, "homepage": "http://expressjs.com/", "keywords": [ "express", "framework", "sinatra", "web", "rest", "restful", "router", "app", "api" ], "dependencies": { "accepts": "~1.2.12", "array-flatten": "1.1.1", "content-disposition": "0.5.0", "content-type": "~1.0.1", "cookie": "0.1.3", "cookie-signature": "1.0.6", "debug": "~2.2.0", "depd": "~1.0.1", "escape-html": "1.0.2", "etag": "~1.7.0", "finalhandler": "0.4.0", "fresh": "0.3.0", "merge-descriptors": "1.0.0", "methods": "~1.1.1", "on-finished": "~2.3.0", "parseurl": "~1.3.0", "path-to-regexp": "0.1.7", "proxy-addr": "~1.0.8", "qs": "4.0.0", "range-parser": "~1.0.2", "send": "0.13.0", "serve-static": "~1.10.0", "type-is": "~1.6.6", "utils-merge": "1.0.0", "vary": "~1.0.1" }, "devDependencies": { "after": "0.8.1", "ejs": "2.3.3", "istanbul": "0.3.17", "marked": "0.3.5", "mocha": "2.2.5", "should": "7.0.2", "supertest": "1.0.1", "body-parser": "~1.13.3", "connect-redis": "~2.4.1", "cookie-parser": "~1.3.5", "cookie-session": "~1.2.0", "express-session": "~1.11.3", "jade": "~1.11.0", "method-override": "~2.3.5", "morgan": "~1.6.1", "multiparty": "~4.1.2", "vhost": "~3.0.1" }, "engines": { "node": ">= 0.10.0" }, "files": [ "LICENSE", "History.md", "Readme.md", "index.js", "lib/" ], "scripts": { "test": "mocha --require test/support/env --reporter spec --bail --check-leaks test/ test/acceptance/", "test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --require test/support/env --reporter spec --check-leaks test/ test/acceptance/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --require test/support/env --reporter dot --check-leaks test/ test/acceptance/", "test-tap": "mocha --require test/support/env --reporter tap --check-leaks test/ test/acceptance/" }, "gitHead": "ef7ad681b245fba023843ce94f6bcb8e275bbb8e", "bugs": { "url": "https://github.com/strongloop/express/issues" }, "_id": "express@4.13.3", "_shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3", "_from": "express@*", "_npmVersion": "1.4.28", "_npmUser": { "name": "dougwilson", "email": "doug@somethingdoug.com" }, "maintainers": [ { "name": "tjholowaychuk", "email": "tj@vision-media.ca" }, { "name": "jongleberry", "email": "jonathanrichardong@gmail.com" }, { "name": "dougwilson", "email": "doug@somethingdoug.com" }, { "name": "rfeng", "email": "enjoyjava@gmail.com" }, { "name": "aredridel", "email": "aredridel@dinhe.net" }, { "name": "strongloop", "email": "callback@strongloop.com" }, { "name": "defunctzombie", "email": "shtylman@gmail.com" } ], "dist": { "shasum": "ddb2f1fb4502bf33598d2b032b037960ca6c80a3", "tarball": "http://registry.npmjs.org/express/-/express-4.13.3.tgz" }, "directories": {}, "_resolved": "https://registry.npmjs.org/express/-/express-4.13.3.tgz", "readme": "ERROR: No README data found!" }
Package.json Attribute Description
name - package name.
#version - The version number of the package.
description - Description of the package.
homepage - The official website url of the package.
author - The name of the author of the package.
contributors - The names of other contributors to the package.
dependencies - List of dependent packages. If the dependent package is not installed, npm will automatically install the dependent package in the node_module directory.
repository - The type of place where the package code is stored, it can be git or svn, git can be on Github.
main - The main field is a module ID that points to the main project of your program. That is, if the name of your package is express, and the user installs it, then require("express").
keywords - Keywords
Uninstall module
We can use the following command to uninstall the Node.js module.
$ npm uninstall express
After uninstalling, you can go to the /node_modules/ directory to check whether the package still exists, or use the following command to check:
$ npm ls
Update Module
We can update the module using the following command:
$ npm update express
Search module
Use the following to search for the module:
$ npm search express
Create Module
To create a module, the package.json file is essential. We can use NPM to generate a package.json file, and the generated file contains the basic results.
$ npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help json` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg> --save` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. name: (node_modules) php # 模块名 version: (1.0.0) description: Node.js 测试模块(www.php.cn) # 描述 entry point: (index.js) test command: make test git repository: https://github.com/php/php.git # Github 地址 keywords: author: license: (ISC) About to write to ……/node_modules/package.json: # 生成地址 { "name": "php", "version": "1.0.0", "description": "Node.js 测试模块(www.php.cn)", …… } Is this ok? (yes) yes
You need to enter the above information according to your own situation. After entering "yes" at the end, the package.json file will be generated.
Next we can use the following command to register a user in the npm resource library (use email to register):
$ npm adduser Username: mcmohd Password: Email: (this IS public) mcmohd@gmail.com
Next we use the following command to publish the module:
$ npm publish
If you have performed the above steps correctly, you can use npm to install it like other modules.
Version number
You will come into contact with the version number when using NPM to download and publish code. NPM uses semantic version numbers to manage code. Here is a brief introduction.
The semantic version number is divided into three digits: X.Y.Z, which represent the major version number, minor version number and patch version number respectively. When the code changes, the version number is updated according to the following principles.
If you just fix the bug, you need to update the Z bit.
If a new function is added, but it is backward compatible, the Y bit needs to be updated.
If there are major changes, it is not backward compatible and X bit needs to be updated.
Version number With this guarantee, when declaring third-party package dependencies, in addition to relying on a fixed version number, you can also rely on a certain range of version numbers. For example, "argv": "0.0.x" means that it depends on the latest version of argv of the 0.0.x series.
For all version number range specification methods supported by NPM, please view the official documentation.
Common NPM commands
In addition to the parts introduced in this chapter, NPM also provides many functions, and there are many other useful fields in package.json.
In addition to viewing the official documentation at npmjs.org/doc/, here are some common NPM commands.
NPM provides many commands, such as install and publish. Use npm help to view all commands.
NPM provides many commands, such as
install
andpublish
. Usenpm help
to view all commands.Use
npm help <command>
to view detailed help for a command, such asnpm help install
.Use
npm install . -g
in the directory wherepackage.json
is located. You can install the current command line program locally first and can be used before publishing. local testing.Use
npm update <package>
to update the corresponding module in thenode_modules
subdirectory under the current directory to the latest version.Use
npm update <package> -g
to update the corresponding command line program installed globally to the latest version.Use
npm cache clear
to clear the NPM local cache, which is used to deal with people who use the same version number to release new versions of code.Use
npm unpublish <package>@<version>
You can unpublish a version of code that you have published.