npm
npm其实是Node.js的包管理工具(package manager),和PHP里的 composer 一样的功能。
因为我们在Node.js上开发时,会用到很多别人写的JavaScript代码。如果我们要使用别人写的某个包,每次都根据名称搜索一下官方网站,下载代码,解压,再使用,非常繁琐。于是一个集中管理的工具应运而生:大家都把自己开发的模块打包后放到npm官网上,如果要使用,直接通过npm安装就可以直接用,不用管代码存在哪,应该从哪下载。
更重要的是,如果我们要使用模块A,而模块A又依赖于模块B,模块B又依赖于模块X和模块Y,npm可以根据依赖关系,把所有依赖的包都下载下来并管理起来。否则,靠我们自己手动管理,肯定既麻烦又容易出错。
npm已经在Node.js安装的时候顺带装好了。我们在命令提示符或者终端输入npm -v
可以看到当前 npm 的版本信息。
安装模块
npm install <Module Name>
安装好之后,模块的包就放在了工程目录下的 node_modules 下。
全局安装与本地安装
npm 的包安装分为本地安装(local)、全局安装(global)两种,从敲的命令行来看,差别只是有没有-g而已,比如
npm install express # 本地安装
npm install express -g # 全局安装
本地安装
- 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modules 目录。
- 可以通过 require() 来引入本地安装的包。
全局安装
- 将安装包放在 /usr/local 下或者你 node 的安装目录。
- 可以直接在命令行里使用。
如果希望具备两者功能,则需要在两个地方安装它或使用 npm link。
查看安装信息
npm list -g # 查看所有全局安装的模块
npm list grunt # 查看某个模块的版本信息
使用 package.json
package.json 位于模块的目录下,用于定义包的属性。内容可能如下:
{
"name": "test",
"version": "1.0.0",
"description": "test",
"main": "index.js",
"scripts": {
"test": "test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/laravel/elixir.git"
},
"keywords": [
"test"
],
"author": "sunyu",
"license": "MIT",
"bugs": {
"url": "https://github.com/laravel/elixir/issues"
},
"homepage": "https://github.com/laravel/elixir#readme",
"devDependencies": {
"gulp": "^3.9.1"
}
}
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
创建模块
创建模块,package.json 文件是必不可少的。我们可以使用 NPM 生成 package.json 文件,生成的文件包含了基本的结果。
初始化信息
sunyu:test sunyu$ 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>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (test)
version: (1.0.0)
description: test
entry point: (index.js)
test command: test
git repository: https://github.com/laravel/elixir
keywords: test
author: sunyu
license: (ISC) test
Sorry, license should be a valid SPDX license expression (without "LicenseRef"), "UNLICENSED", or "SEE LICENSE IN <filename>".
license: (ISC) MIT
About to write to /Users/sunyu/Documents/workspace/sunyu/test/package.json:
{
"name": "test",
"version": "1.0.0",
"description": "test",
"main": "index.js",
"scripts": {
"test": "test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/laravel/elixir.git"
},
"keywords": [
"test"
],
"author": "sunyu",
"license": "MIT",
"bugs": {
"url": "https://github.com/laravel/elixir/issues"
},
"homepage": "https://github.com/laravel/elixir#readme"
}
Is this ok? (yes) yes
以上的信息,你需要根据你自己的情况输入。在最后输入 “yes” 后会生成 package.json 文件。要注意的是 git repository 这里的地址。
添加依赖
npm install --save-dev gulp
它会把 gulp 作为依赖添加到配置文件中,且是添加到 devDependencies中,如果要添加到 dependencies,需要用 --save-prod
:
npm install --save-prod gulp --registry=https://registry.npm.taobao.org
注册用户
$ npm adduser
Username: sun86yu
Password:
Email: (this IS public) sun86yu@gmail.com
发布模块
npm publish
版本号
使用NPM下载和发布代码时都会接触到版本号。NPM使用语义版本号来管理代码,语义版本号分为X.Y.Z三位,分别代表主版本号、次版本号和补丁版本号。当代码变更时,版本号按以下原则更新:
- 如果只是修复bug,需要更新Z位。
- 如果是新增了功能,但是向下兼容,需要更新Y位。
- 如果有大变动,向下不兼容,需要更新X位
版本号有了这个保证后,在申明第三方包依赖时,除了可依赖于一个固定版本号外,还可依赖于某个范围的版本号。例如”argv”: “0.0.x”表示依赖于0.0.x系列的最新版argv。
使用淘宝 NPM 镜像
国内直接使用 npm 的官方镜像是非常慢的,这里推荐使用淘宝 NPM 镜像。
淘宝 NPM 镜像是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步。
你可以使用淘宝定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:
npm install -g cnpm --registry=https://registry.npm.taobao.org
这样就可以使用 cnpm 命令来安装模块了:
cnpm install [name]