0x001 概述
npm是一个包管理工具,本文章基本算是对官方文档的解读、翻译和实操笔记,顺便说明使用npm中的坑。
0x002 安装Node.js
安装方式参照Node.js
官方文档即可,安装完成后,命令行键入node -v
成功执行说明安装成功了
$ node -v
v6.3.1
顺便检查一下npm
版本并更新它
$ npm -v
5.3.0
$ npm install npm@latest -g
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
/usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
+ npm@5.4.1
added 22 packages, removed 22 packages and updated 133 packages in 11.208s
╭─────────────────────────────────────╮
│ │
│ Update available 5.3.0 → 5.4.1 │
│ Run npm i -g npm to update │
│ │
╰─────────────────────────────────────╯
0x003 本地安装包
新建一个项目文件夹
mkdir 0x003-install-local
cd 0x003-install-local
安装loadash
$ npm install lodash
npm WARN saveError ENOENT: no such file or directory, open '/MY_PROJECT/PROJECT_OWN/NodeJS/npm/package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open '/MY_PROJECT/PROJECT_OWN/NodeJS/npm/package.json'
npm WARN npm No description
npm WARN npm No repository field.
npm WARN npm No README data
npm WARN npm No license field.
+ lodash@4.17.4
added 1 package in 0.862s
成功安装包之后,将会发生以下的事情:
-
在项目的目录会生成一个
node_modules
文件夹,里面放置了我们项目所有的本地依赖包$ cd node_modules $ ls lodash
如果该项目存在
package.json
文件,将会把该lodash
加入到项目的依赖中,这里没有package.json,所以安装的时候有个WARN
。生成一个
package-lock.json
,将项目的依赖版本锁定,这样迁移的时候,重新安装依赖就会安装指定版本的包(新版本才有,待验证)。
引用并执行
$ vim index.js
// index.js
var lodash = require('lodash');
var output = lodash.without([1, 2, 3], 1);
console.log(output);
$ node index.js
[ 2, 3 ]
如果安装出错,比如写错了包名,将会发生以下事情:
命令行报错
npm install loadashsh
npm ERR! code E404
npm ERR! 404 Not Found: loadashsh@latest
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/FollowWinter/.npm/_logs/2017-09-10T11_37_44_504Z-debug.log
0x004 使用package.json
可以自己新建一个文件编写package.json
,也可以直接使用npm init
来初始化一个
$ mkdir 0x004-use-package_json
$ cd 0x004-use-package_json/
$ 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: (0x004-use-package_json)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /MY_PROJECT/PROJECT_OWN/NodeJS/npm/0x004-use-package_json/package.json:
{
"name": "0x004-use-package_json",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Is this ok? (yes)
该命令是一个交互式命令,可以根据提示输入相应信息生成相应的package.json
,命令提示后面的()
中为提示内容,可以选择一路enter
,然后再去修改package.json
。执行成功后,将会在项目中生成package.json
文件。
// package.json
{
"name": "0x004-use-package_json",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
再一次安装包
$ npm install lodash
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN 0x004-use-package_json@1.0.0 No description
npm WARN 0x004-use-package_json@1.0.0 No repository field.
+ lodash@4.17.4
added 1 package in 1.378s
//package.json
{
"name": "0x004-use-package_json",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.4"
}
}
可以发现,多了一个dependencies
节点,该节点保存了项目所有的依赖,当然我们也可以通过对该节点的修改,改变项目的依赖,比如删除lodash
,或者改变lodash
版本。
删除dependencies
中的lodash
之后再执行
$ npm install
npm WARN 0x004-use-package_json@1.0.0 No description
npm WARN 0x004-use-package_json@1.0.0 No repository field.
removed 1 package in 0.249s
$ cd node_modules
$ ls -al
. ..
可以看出,lodash
包已经被移除npm install
可以根据package.json
的指示安装、更新、删除依赖。
0x005 更新依赖
npm update
0x006 删除依赖
$ npm uninstall lodash
npm WARN 0x004-use-package_json@1.0.0 No description
npm WARN 0x004-use-package_json@1.0.0 No repository field.
removed 1 package in 0.247s
0x007 全局包管理
和安装本地包没有区别,只是加了个-g
参数,表示global
$ npm install -g jshint
/usr/local/bin/jshint -> /usr/local/lib/node_modules/jshint/bin/jshint
+ jshint@2.9.5
added 31 packages in 41.728s
同理包的更新和删除也无异,
$ npm install -g jshint
$ npm update -g jshint
$ npm uninstall -g jshint
removed 31 packages in 0.365s
0x008 总结
一篇文章太长,打起字来就会卡顿, 我打了很多字吗?
npm init
: 初始化项目,生成package.json
-
npm install [<package_name> [--save|--save-dev] -g]
:根据项目中的
package.json
初始化项目、更新依赖如果指定了
<package_name>
,则安装<package_name>
-
npm update [<package_name>]
:更新项目依赖到新版本
如果指定了
<package_name>
,则更新<package_name>
npm uninstall <package_name>
: 移除<package_name>
依赖,并更新dependencies
或者dependencies-dev
中的<package_name>
节点
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。