NPM 使用介绍
NPM 是随同NodeJS一起安装的包管理工具,能够解决NodeJS 代码部署上的很多问题,常见的使用场景有以下几种:
* 允许用户从NPM 服务器上下载别人编写的第三方包到本地使用。
* 允许用户从NPM 服务器上下载并安装别人编写的命令行程序到本地使用。
* 允许用户将自己编写的包或命令行程序上传到NPM 服务器上供别人使用。
查看NPM 版本
> npm -v
旧版NPM 升级
> sudo npm install npm -g
淘宝镜像安装
cnpm install npm -g
安装模块
> npm install express
包安装在工程目录下的node_modules目录中,引用时候,无需指定路径。
var express = require('express');
全局安装 与 本地安装
npm 的包安装分为本地安装(local)、全局安装(global)两种。
> npm install express #局部安装
> npm install express -g #全局安装
npm err! Error:connect ECONNREFUSED 127.0.0.1:8087
# npm config set proxy null
本地安装
- 将安装包放在./node_modules下(运行npm命令时所在的目录),如果没有node_modules目录就会在当前执行npm命令的目录下生成node_modules 目录。
- 可以通过require()来引入本地安装的包。
全局安装
- 将安装包放在 /usr/local 下或者node 的安装目录
- 可以直接在命令行里使用。
- 如果希望具备两者的功能,则需要在两个地方进行安装,或使用 npm link。
查看模块安装信息
> npm list #local
> npm list -g #global
查看某个模块的安装信息
> npm list express -g #global
> npm list express #local
package.json
{
"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 属性说明
- name – 包名
- version – 包的版本号
- description – 包的描述
- homepage – 包的官网 url
- author – 包的作者姓名
- contributors – 包的其他贡献者姓名
- dependencies – 依赖包列表
- respository – 包代码存放地方的类型,可以是git或svn,git可以在 Github上。
- main – main 字段制定了程序的主入口文件,require('package-name')就会加载这个文件。字段默认值是模块根目录下面的index.js。
- keywords – 关键字
卸载模块
> npm uninstall express
查看模块
> npm list
更新模块
> npm update express
搜索模块
> npm search express
创建模块
> 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) fe.docs # 模块名
version: (1.0.0)
description: 网页文档 # 描述
entry point: (index.js)
test command:
git repository: https://github.com/daysunx2/fe.docs.git # Github 地址
keywords:
author:
license: (ISC)
About to write to ……/node_modules/package.json: # 生成地址
{
"name": "fe.docs",
"version": "1.0.0",
"description": "网页文档",
……
}
Is this ok? (yes) yes
> npm adduser
> Username: day
> Password: ******
> Email: (this IS public)xxx@qq.com
发布项目
> npm publish
版本号
使用NPM 下载和发布代码时都会接触到版本号,NPM 使用语义版本号来管理代码。
语义版本号分为 x.y.z 三位,分别代表主版本号、次版本号、和补丁版本号。当代码变更时,版本号应按照以下原则进行更新。
- 如果只是修复bug,需要更新Z 位
- 如果是新增了功能,但是向下兼容,需要更新Y位。
- 如果有大的变动,向下不兼容,则需要更新X位。
NPM 常用命令
- NPM 提供了很多的命令 如 install , publish ,使用 npm help 可以查看所有命令。
npm help <commond>
可查看某条命令的详细帮助,例如 npm help install。- 在package.json 所在的目录下使用
npm install . -g
可先在本地安装当前命令行程序,可用于发布前的本地测试。 - 使用
npm update <package>
可以把当前目录下的node_modules子目录里边的对应模块更新至最新版本。 npm update <package> -g
可以把全局安装的对应命令行程序更新至最新版。npm cache clear
可以清空NPM本地缓存,用于对付使用相同版本号发布新版本代码的人。npm unpublish <package>@<version>
可以撤销发布自己发布过的某个版本代码。
使用淘宝NPM 镜像
使用cnpm(gzip压缩支持)命令行工具代替默认的npm:
> npm install -g cnpm --registry=https://registry.npm.taobao.org
> cnpm install [name]