NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种:
- 允许用户从NPM服务器下载别人编写的第三方包到本地使用。
- 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。
允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用。
安装
Node附带的NPM可能不是最新版本,最好用
npm install npm -g更新下- 默认安装到当前系统Node所在目录下
- 由于之前使用NVM的方式安装的Node所以需要重新配置NPM的全局目录
使用 npm 命令安装模块
npm install
全局安装与本地安装
npm 的包安装分为本地安装(local)、全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如
npm install express # 本地安装 npm install express -g # 全局安装
查看安装信息
你可以使用以下命令来查看所有全局安装的模块:
npm list -g
如果要查看某个模块的版本号,可以使用命令如下:
npm list grunt
使用 package.json
package.json 位于模块的目录下,用于定义包的属性。接下来让我们来看下 express 包的 package.json 文件,位于 node_modules/express/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 - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
- repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。
- main - main 字段指定了程序的主入口文件,require(‘moduleName’) 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。
- keywords - 关键字
卸载模块
$ npm uninstall express
更新模块
$ 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 jsonfor definitive documentation on these fields and exactly what they do.Use
npm install <pkg> --saveafterwards 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) runoob # 模块名 version: (1.0.0) description: Node.js 测试模块(www.runoob.com) # 描述 entry point: (index.js) test command: make test git repository: https://github.com/runoob/runoob.git # Github 地址 keywords: author: license: (ISC) About to write to ……/node_modules/package.json: # 生成地址
{ “name”: “runoob”, “version”: “1.0.0”, “description”: “Node.js 测试模块(www.runoob.com)”, …… }
Is this ok? (yes) yes
可以使用以下命令在 npm 资源库中注册用户(使用邮箱注册):
$ npm adduser Username: mcmohd Password: Email: (this IS public) mcmohd@gmail.com
用以下命令来发布模块
$ npm publish
nrm
nrm(npm registry manager )是npm的镜像源管理工具,有时候国外资源太慢,使用这个就可以快速地在 npm 源间切换
安装
在命令行执行命令,npm install -g nrm,全局安装nrm。
nrm ls查看可选的源。
nrm ls
切源
nrm use taobao
添加
执行命令 nrm add
,其中reigstry为源名,url为源的路径。
删除
nrm del
测速
nrm test npm
