文章目录
0 NPM(Node Package Manager)
NPM:node包管理工具,它是一个命令行工具,也是第三方库注册到node的注册机。
在向node添加任何包之前,需要创建一个文件,package.json
。它包含了应用或程序最基本的信息,比如名字、版本、作者、反馈的地址、依赖关系等等。
所有node都应有这个json文件。
npm init
package name: (npm-demo)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to C:\Users\Zijeak\Desktop\npm-demo\package.json:
{
"name": "npm-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this ok? (yes)
1 如何安装一个包?
在官网https://www.npmjs.com/找到需要安装的包,复制安装命令,在命令行安装即可。
npm install underscore
安装完成后,我们看到,package.json
中多了一个依赖关系
{
"name": "npm-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"underscore": "^1.10.2"
}
}
2 如何使用一个包?
先来了解一下,node是如何查找到一个模块的?
当我们在一个模块内使用require引用另一个模块var _ = require('underscore');
require
函数的查找顺序如下:
- 核心模块
- 文件或文件夹:如果参数是
'./underscore'
,那么reqiure
函数就会认为在同一个文件夹内有underscore.js
文件,如果不是,他就会认为在underscore
文件夹内有一个index.js
文件 - node_modules:在
node_modules
文件夹中,有一个underscore
库
我们可以在包的官网文档中查找到它的使用方法:https://underscorejs.org/
文档中关于contains方法的解释如下:
接下来我们在程序中使用它:
var _ = require('underscore');
console.log(_.contains([1,2,3],3));//true
3 NPM的包与源代码管理
我们的包依赖关系都保存在文件 package.json 中,因此,我们可以轻松地在任何机器、任何地点将依赖库恢复过来,从而在管理远程源代码时无需上传数量繁多的依赖库文件。
npm i
这个命令会从检查package.json
文件,然后从npm注册库中下载所有的依赖
那么,如何在使用Git管理源码是避免上传依赖库呢?
做法是,在项目根目录创建一个.gitignore
文件夹
里面写入依赖文件夹的路径即可
4 语义化版本控制
依赖库的版本号:主版本号.次版本号.补丁号
版本号之前的
^
符号意为:每次运行npm i
时,只要主版本号没有发生变化,则自动安装更新;例如^5.x.x
版本号之前的~
符号意为:每次运行npm i
时,只要主版本号和次版本号没有发生变化,则自动安装更新;例如~5.9.x
版本号之前的没有任何符号意为:每次运行npm i
时,只安装特定版本的依赖库,例如5.9.7
5 查看依赖库的版本
npm list
+-- mongoose@5.9.7
| +-- bson@1.1.4
| +-- kareem@2.3.1
| +-- mongodb@3.5.5
| | +-- bl@2.2.0
| | | +-- readable-stream@2.3.7
| | | | +-- core-util-is@1.0.2
| | | | +-- inherits@2.0.4
| | | | +-- isarray@1.0.0
| | | | +-- process-nextick-args@2.0.1
| | | | +-- safe-buffer@5.1.2 deduped
| | | | +-- string_decoder@1.1.1
| | | | | `-- safe-buffer@5.1.2 deduped
| | | | `-- util-deprecate@1.0.2
| | | `-- safe-buffer@5.1.2 deduped
| | +-- bson@1.1.4 deduped
| | +-- denque@1.4.1
| | +-- require_optional@1.0.1
| | | +-- resolve-from@2.0.0
| | | `-- semver@5.7.1
| | +-- safe-buffer@5.1.2 deduped
| | `-- saslprep@1.0.3
| | `-- sparse-bitfield@3.0.3
| | `-- memory-pager@1.5.0
| +-- mongoose-legacy-pluralize@1.0.2
| +-- mpath@0.6.0
| +-- mquery@3.2.2
| | +-- bluebird@3.5.1
| | +-- debug@3.1.0
| | | `-- ms@2.0.0
| | +-- regexp-clone@1.0.0 deduped
| | +-- safe-buffer@5.1.2 deduped
| | `-- sliced@1.0.1 deduped
| +-- ms@2.1.2
| +-- regexp-clone@1.0.0
| +-- safe-buffer@5.1.2
| +-- sift@7.0.1
| `-- sliced@1.0.1
`-- underscore@1.10.2
npm list --depth=0
+-- mongoose@5.9.7
`-- underscore@1.10.2
6 查看包的注册信息
npm view 库名
仅查看依赖:
npm view 库名 dependencies
查看发行的所有版本号
npm view 库名 versions
7 安装特定版本的包
npm install 包名@版本号
8 升级本地包
快速查看过期依赖
npm outdated
更新(只会更新到依赖包指定更新的版本,例如若依赖包处为^5.9.x
,而最新版本已经是7.9.2
,运行下面命名只会更新到5.9.x
的最新版本)
npm update
强制更新为最新版本(全局安装)
npm i -g npm-check-updates
9 开发中使用的依赖库
以js静态代码分析工具jshint为例
npm i jshint --save-dev
10 删除包
npm uninstall 包名
或
npm un 包名
11 操作全局包
-g参数