npm-參考手冊

索引

  • 權限node

    • t/team 組織成員管理git

    • access 包訪問控制github

    • adduser/login 用戶登陸shell

    • logout 註銷express

    • owner 全部者管理npm

    • whoami 查看用戶信息json

  • 包倉儲瀏覽器

    • s/se/search 倉儲查找包緩存

    • publish 發佈bash

    • unpublish 取消發佈

    • deprecate 棄用

    • stars 我喜歡的包

    • star 喜歡

    • unstar 取消喜歡

  • 包本地

    • init 初始化package.json

    • i/install 安裝

    • un/uninstall 刪除

    • dedupe/ddp 清除重複包

    • dist-tags 標籤管理

    • version 更新包的版本信息

    • it/install-test 運行npm install && npm test

    • ln/link 安裝連接

    • ls/list 列出包

    • update/up 更新並安裝遺漏的包

    • outdated 檢測過時

    • pack 打包tarball文件

    • prune 清理外來包

    • shrinkwrap 鎖定依賴包版本

    • cache 緩存管理

  • 腳本

    • run/run-script 運行腳本

    • start 運行start腳本

    • stop 運行stop腳本

    • tst/test 運行test腳本

    • rb/rebuild 從新編譯本地包

    • restart 順序執行重啓相關的一系列腳本

  • 配置

    • c/config 配置管理

    • get 列出配置

    • set 設置配置

  • 查看

    • root 包根目錄

    • prefix 打印prefix配置

    • v/view 查看倉儲信息

    • bin 查看bin目錄

    • bugs/issue 瀏覽器查看bugs

    • docs/home 瀏覽器查看幫助文檔

    • repo 瀏覽器查看倉儲

    • help 查看幫助

    • help-search 幫助中搜索關鍵字

  • 其餘

    • completion shell插補

    • doctor 環境檢測

    • edit 進入包目錄並啓動編輯器

    • explore 進入包目錄並運行命令

    • ping 檢查倉儲是否可用

權限

npm容許經過scope組織私有包,經過team細化權限控制.

npm官方倉儲有兩種類型的包,普通包和scope包

普通包特徵:

  • 只能公有,誰均可如下載使用

  • 僅能夠經過全部者(owner)進行權限控制,若是要容許某個用戶修改或發佈包,必須將該用戶添加到包的全部者列表。添加到包全部者列表的用戶具有全部的權限.

scope包特徵:

  • 包名有兩部組成,@scope/name, @後的爲scope名,/後的纔是具體的包名

  • 能夠控制公有和私有

  • 細化的權限控制,好比能夠建立團隊,並賦予團隊對包只讀/修改的權限

owner

npm owner add <user> [<@scope>/]<pkg> # 將用戶添加到包的全部者列表
npm owner rm <user> [<@scope>/]<pkg> # 從包的全部這列表中刪除用戶
npm owner ls [<@scope>/]<pkg> # 列出包的全部者

成爲包的全部者的用戶,將可以修改元數據(如標記棄用),發佈新版本,添加其餘用戶到包的全部者列表

t/team

npm team create <scope:team> # 建立團隊
npm team destroy <scope:team> # 刪除團隊

npm team add <scope:team> <user> # 添加用戶到團隊
npm team rm <scope:team> <user> # 從團隊中移除用戶 

npm team ls <scope>|<scope:team> 列出團隊/成員

npm team edit <scope:team>  用編輯器編輯團隊信息

access

npm access public [<package>]  # 設置包開放
npm access restricted [<package>] # 設置包私有

npm access grant <read-only|read-write> <scope:team> [<package>] # 設置團隊對包能夠只讀/容許修改
npm access revoke <scope:team> [<package>] # 從團隊中收回包權限

npm access ls-packages [<user>|<scope>|<scope:team>]  # 列出用戶/域/團隊可以訪問的包
npm access ls-collaborators [<package> [<user>]] # 列出包的權限信息
npm access edit [<package>] # 用編輯器編輯包權限

adduser/login

npm adduser [--registry=url] [--scope=@orgname] [--always-auth]

提示輸入username, password, email,進行登陸校驗,返回token保存到.npmrc

logout

npm logout [--registry=<url>] [--scope=<@scope>]

請求倉儲服務將當前token失效

whoami

npm whoami [--registry <registry>]

列出用戶在npmjs.org上的用戶名

包倉儲

s/se/search

npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms ...]
  • -l|--long: 展現出所有的DESCRIPTION欄信息

  • --no-description: 不顯示DESCRIPTION欄信息

publish

npm publish [<tarball>|<folder>] [--tag <tag>] [--access <public|restricted>]
  • --tag: 帶上tag信息發佈,以後包能夠經過npm install <name>@<tag>安裝

  • --access: 僅適用於scope包,默認爲restricted

unpublish

npm unpublish [<@scope>/]<pkg>[@<version>]

從倉儲中刪除包,該操做會破壞依賴,不推薦適用,若是是爲了鼓勵用戶適用新版本,可使用deprecate命令

deprecate

npm deprecate <pkg>[@<version>] <message>

標記包棄用,用戶在安裝時npm會有警告

stars

npm stars [<user>]

查看用戶喜歡的包

star/unstart

npm star [<pkg>...]
npm unstar [<pkg>...]

標記喜歡/取消喜歡標記

包本地

init

npm init [-f|--force|-y|--yes]

初始化package.json, 默認會有不少輸入提示,能夠經過-f|--force|-y|--yes選項建立默認配置的package.json
已經存在package.json後再次運行npm init不會破壞已有配置,只會變動你真正改動的部分

i/install

npm install (with no args, in package dir) # 讀取package.json安裝
npm install [<@scope>/]<name> # 默認安裝標籤爲latest
npm install [<@scope>/]<name>@<tag> # 指定標籤
npm install [<@scope>/]<name>@<version> # 指定版本
npm install [<@scope>/]<name>@<version range> # 指定版本範圍
npm install <tarball file>  # 經過tarball文件安裝
npm install <tarball url> # 經過tarball文件url連接安裝
npm install <git remote url> # 經過git安裝包, url格式爲<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish>]
npm install <folder> 經過包所在的文件夾安裝
  • --registry: 從指定倉儲中下載安裝包

  • -S/--save: 安裝並保存包信息到package.json的dependencies區

  • -D/--save-dev: 安裝並保存包信息到package.json的devDependencies區

  • --tag: 優先根據標籤而不是版本安裝包

  • --dry-run: 報告安裝情況而不真的安裝

  • -f/--force: 安裝時跳過緩存直接從遠程下載

  • -g/--global: 安裝到全局

  • --link: 連接全局安裝的包的本地

  • --no-shrinkwrap: 安裝時忽略shrinkwrap

un/uninstall

npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|-D|--save-dev]
  • -S/--save: 刪除包並移除包在package.json的dependencies區的信息

  • -D/--save-dev: 刪除包並移除包在package.json的devDependencies區的信息

ddp/dedupe

npm dedupe

npm檢查包依賴樹並清除不要的包

dist-tags

npm dist-tag add <pkg>@<version> [<tag>] # 添加標籤
npm dist-tag rm <pkg> <tag> # 移除標籤
npm dist-tag ls [<pkg>] # 列出包所包含的標籤

常見標籤有latest, next, lts等

能夠在發佈和下載包是帶上標籤

npm publish # 默認標籤latest
npm publish --tag next  # 發佈next標籤 
npm install npm # 默認標籤latest
npm install npm@next  
npm install --tag next
  • --registry: 發佈包到指定倉儲

v/version

npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]

該命令執行步驟

  1. 檢查git工做目錄

  2. 運行preversion腳本, 能夠寫些觸發測試的腳本

  3. 結合當前包當前版本信息和patch, minor, major等,生成新版本號,更新package.json中version字段

    • patch 1.0.0 => 1.0.1

    • prepatch 1.0.0 => 1.0.1-0

    • minor 1.0.0 => 1.1.0

    • preminor 1.0.0 => 1.1.0-0

    • major 1.0.0 => 2.0.0

    • premajor 1.0.0 => 2.0.0-0

    • prerelease 1.0.0-0 => 1.0.0-1

    • from-git 從git獲取版本信息

  4. 運行version腳本

  5. git提交併打標籤

  6. 運行postversion腳本

it/install-test

npm it
npm install-test

至關於運行npm install && npm test

ln/link

npm link  # 在全局node_modules下建立當前文件夾的超連接
npm link [<@scope>/]<pkg>[@<version>] # 將全局安裝的包連接到本地node_modules中

ls/list

npm ls [[<@scope>/]<pkg> ...]

打印依賴樹

  • --json: 已json格式輸出

  • --long: 展現更多信息

  • --parseable: 顯示展平的目錄而不是依賴樹

  • --global: 顯示全局安裝的包的依賴樹

  • --depth: 樹層級,從0開始

  • --prod/production: 僅顯示package.json裏dependencies包的依賴

  • --dev: 僅顯示package.json裏devDependencies包的依賴

up/update

npm update [-g] [<pkg>...]

更新包到包的semver所容許的最新版本, 並安裝遺漏的包

  • --save: 更新並保存更新到package.json

  • --dev: 同時更新devDependencies中的包

  • --depth: 默認狀況下僅更新頂層(--depth=0)爲0的包,若是想更新全部包,能夠指定--depth=9999

outdated

npm outdated [[<@scope>/]<pkg> ...]

.e.g

Package        Current  Wanted  Latest  Location
ajv              4.8.2  4.11.8   5.0.1  example
async            2.1.2   2.4.0   2.4.0  example
body-parser     1.15.2  1.17.1  1.17.1  example

列表欄

  • Current: 當前版本

  • Wanted: smever容許的最高版本

  • Latest: 倉儲中最新版本

  • Location: 依賴樹中的位置

命令選項

  • --json: 已json格式輸出

  • --long: 展現更多信息

  • --parseable: 平鋪展現

  • --global: 顯示全局安裝的包的依賴樹

  • --depth: 樹層級,默認0

pack

npm pack [[<@scope>/]<pkg>...]

從包生成名爲<name>-<version>.tgz的tarball,並緩存

prune

npm prune [[<@scope>/]<pkg>...] [--production]

清理不在package.json生成的依賴樹中的包

  • --production: 移除devDependencies中的包

shrinkwrap

npm shrinkwrap

shrinkwrap用來鎖定依賴包的版本

包A的package.json

{
    "name": "A",
    "version": "0.1.0",
    "dependencies": {
        "B": "<0.1.0"
    }
}

包A的依賴樹

A@0.1.0
    `-- B@0.0.1
        `-- C@0.0.1

當B有新版本0.0.2發佈, B@0.0.2知足<0.1.0, 因此npm install A安裝成功後依賴樹

A@0.1.0
    `-- B@0.0.2
        `-- C@0.0.1

咱們但願包A依賴的B版本保持在B@0.0.1, 能夠運行

npm shrinkwrap

該命令會生成npm-shrinkwrap.json, 其內容以下

{
  "name": "A",
  "version": "0.1.0",
  "dependencies": {
    "B": {
      "version": "0.0.1",
      "from": "B@^0.0.1",
      "resolved": "https://registry.npmjs.org/B/-/B-0.0.1.tgz",
      "dependencies": {
        "C": {
          "version": "0.0.1",
          "from": "org/C#v0.0.1",
          "resolved": "git://github.com/org/C.git#5c380ae319fc4efe9e7f2d9c78b0faa588fd99b4"
        }
      }
    }
  }
}

運行npm install時若是存在npm-shrinkwrap.json, npm在安裝包時會根據shrinkwrap.json鎖定依賴包的版本

cache

npm cache add <tarball file> # 添加到緩存
npm cache add <folder>
npm cache add <tarball url>
npm cache add <name>@<version> 

npm cache ls [<path>]  # 緩存明細

npm cache clean [<path>] # 清除緩存

緩存路徑能夠經過npm config get cache獲取

腳本

package.json的scripts區能夠用來定義自定義腳本

run/run-script

npm run <command> [-- <args>...]

運行package.json的scripts中定義的命令

npm run會自動將node_modules/.bin添加到環境變量PATH中。若是本地安裝過mocha, 能夠這樣編寫"scripts": {"test": "mocha test/\*.js"}而不須要"scripts": {"test": "node_modules/.bin/tap test/\*.js"}

start

npm start [-- <args>]

等同與npm run start [-- <args>]

stop

npm stop [-- <args>]

等同與npm run stop [-- <args>]

tst/test

npm test [-- <args>]

等同與npm run test [-- <args>]

rb/rebuild

npm rebuild [[<@scope>/<name>]...]

運行指定包中的build腳本,適用於更新node版本後,從新編譯C++包

restart

npm restart [-- <args>]

循序執行1. prerestart 2. prestop 3. stop 4. poststop 5. restart 6. prestart 7. start 8. poststart 9. postrestart

配置

c/config

npm config set <key> <value> [-g|--global] # 添加或更新
npm config get <key> # 獲取
npm config delete <key> # 刪除
npm config list #  配置明細
npm config edit # 編輯器編輯
  • --global: 全局配置

get

npm get <key> # 同npm config get

set

npm set <key> <value> [-g|--global] #同npm config set

查看

root

npm root # 打印本地node_modules目錄
npm root -g # 打印全局node_modules目錄

prefix

npm prefix # 打印包含package.json最近父目錄
npm prefix -g # 打印全局配置prefix的值

view

npm view [<@scope>/]<name>[@<version>] [<field>[.<subfield>]...]

查看倉儲信息

npm view compact


#  打印
{ name: 'compact',
  description: 'A JavaScript compacting middleware for express',
  'dist-tags': { latest: '0.1.2' },
  maintainers: [ 'serby <paul@serby.net>' ],
  time: 
   { modified: '2017-03-28T12:49:48.000Z',
     created: '2012-02-06T01:39:50.261Z',
     '0.1.2': '2012-09-04T11:19:17.618Z',
     '0.1.1': '2012-08-29T08:18:12.345Z',
     '0.1.0': '2012-07-09T14:40:56.751Z',
     '0.0.7': '2012-07-04T17:14:01.593Z',
     '0.0.6': '2012-06-29T14:29:04.847Z',
     '0.0.5': '2012-05-23T10:10:15.010Z',
     '0.0.4': '2012-03-31T09:05:40.450Z',
     '0.0.3': '2012-03-23T15:25:18.289Z',
     '0.0.2': '2012-03-21T18:15:24.718Z',
     '0.0.1': '2012-02-06T01:39:50.261Z' },
  users: { serby: true },
  author: 'Paul Serby <paul@serby.net>',
  repository: { type: 'git', url: 'git://github.com/serby/compact.git' },
  versions: 
   [ '0.0.1',
     '0.0.2',
     '0.0.3',
     '0.0.4',
     '0.0.5',
     '0.0.6',
     '0.0.7',
     '0.1.0',
     '0.1.1',
     '0.1.2' ],
  version: '0.1.2',
  main: './lib/compact.js',
  scripts: { test: 'mocha -r should -R spec' },
  engines: { node: '>=0.8' },
  dependencies: 
   { lodash: '~0.3',
     async: '~0.1',
     'uglify-js': '~1.3',
     mkdirp: '~0.3' },
  devDependencies: { mocha: '*', should: '~1.1', async: '~0.1', asyncjs: '~0.0' },
  optionalDependencies: {},
  dist: 
   { shasum: '66361e17108185bf261d42aff6a91b925e473139',
     size: 7603,
     noattachment: false,
     tarball: 'http://registry.npm.taobao.org/compact/download/compact-0.1.2.tgz' },
  directories: {},
  publish_time: 1346757557618 }
npm view compact@0.1.2 dependencies

# 打印
{ lodash: '~0.3',
  async: '~0.1',
  'uglify-js': '~1.3',
  mkdirp: '~0.3' }

bin

npm bin # 打印包含npm bin目錄, 一般爲node_modules/.bin/
npm prefix -g # 打印全局npm bin目錄

bugs/issue

npm bugs [<packagename>]

打開包bug追蹤url

npm bugs npm # 瀏覽器打開https://github.com/npm/npm/issues

docs/home

npm docs [<pkgname> [<pkgname> ...]]
npm docs .
npm home [<pkgname> [<pkgname> ...]]
npm home .

打開文檔url

npm docs npm #瀏覽器打開https://docs.npmjs.com/

repo

npm repo [<pkg>]

打開git url

npm repo npm #瀏覽器打開https://github.com/npm/npm

help

npm help <term> [<terms..>]

打印特定術語或命令的幫助

help-search

npm help-search <text>

從npm官方markdown文檔中搜索詞條

其餘

completion

npm completion >> ~/.bashrc

npm命令插補腳本

doctor

npm doctor

環境檢測

  • npm能調用node和git命令

  • registry可以訪問

  • 本地和全局node_modules可寫

  • 緩存存在且tarball文件健全

edit

npm edit <pkg>[@<version>]

進入包目錄並啓動編輯器

explore

npm explore <pkg> [-- <cmd>]

進入包目錄並運行命令

npm explore connect -- ls

# 打印
HISTORY.md  index.js  LICENSE  node_modules  package.json  README.md

ping

npm ping [--registry <registry>]
npm ping --registry https://registry.npmjs.org

檢查倉儲是否可用

更多文章的目錄在這裏

相關文章
相關標籤/搜索