npm是一个包管理工具,本文章基本算是对官方文档的解读、翻译和实操笔记,顺便说明使用npm中的坑。node
Node.js
安装方式参照Node.js
官方文档便可,安装完成后,命令行键入node -v
成功执行说明安装成功了git
$ node -v v6.3.1
顺便检查一下npm
版本并更新它github
$ 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 │ │ │ ╰─────────────────────────────────────╯
新建一个项目文件夹npm
mkdir 0x003-install-local cd 0x003-install-local
安装loadash
json
$ 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
成功安装包以后,将会发生如下的事情:vim
在项目的目录会生成一个node_modules
文件夹,里面放置了咱们项目全部的本地依赖包工具
$ cd node_modules $ ls lodash
若是该项目存在package.json
文件,将会把该lodash
加入到项目的依赖中,这里没有package.json,因此安装的时候有个WARN
。ui
生成一个package-lock.json
,将项目的依赖版本锁定,这样迁移的时候,从新安装依赖就会安装指定版本的包(新版本才有,待验证)。this
引用并执行命令行
$ 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
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
的指示安装、更新、删除依赖。
npm update
$ 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
和安装本地包没有区别,只是加了个-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
一篇文章太长,打起字来就会卡顿, 我打了不少字吗?
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>
节点