npm入手筆記0x001-項目初始化和包增刪改

0x001 概述

npm是一個包管理工具,本文章基本算是對官方文檔的解讀、翻譯和實操筆記,順便說明使用npm中的坑。node

0x002 安裝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      │
   │                                     │
   ╰─────────────────────────────────────╯

0x003 本地安裝包

新建一個項目文件夾npm

mkdir 0x003-install-local
cd 0x003-install-local

安裝loadashjson

$ 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,因此安裝的時候有個WARNui

  • 生成一個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

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>節點

0x009 資源

相關文章
相關標籤/搜索