就像NPM的官網(http://npmjs.org/)上介紹的那樣,安裝NPM僅僅是一行命令的事情:node
curl http://npmjs.org/install.sh | sh
這裏詳解一下這句命令的意思,curl http://npmjs.org/install.sh是經過curl命令獲取這個安裝shell腳本,按後經過管道符| 將獲取的腳本交由sh命令來執行。這裏若是沒有權限會安裝不成功,須要加上sudo來確保權限:shell
curl http://npmjs.org/install.sh | sudo sh
安裝成功後執行npm命令,會獲得一下的提示:npm
Usage: npm <command> where <command> is one of: adduser, apihelp, author, bin, bugs, c, cache, completion, config, deprecate, docs, edit, explore, faq, find, get, help, help-search, home, i, info, init, install, la, link, list, ll, ln, ls, outdated, owner, pack, prefix, prune, publish, r, rb, rebuild, remove, restart, rm, root, run-script, s, se, search, set, show, star, start, stop, submodule, tag, test, un, uninstall, unlink, unpublish, unstar, up, update, version, view, whoami
咱們以underscore爲例,來展現下經過npm安裝第三方包的過程。api
npm install underscore
返回:服務器
underscore@1.2.2 ./node_modules/underscore
因爲一些特殊的網絡環境,直接經過npm install命令安裝第三方庫的時候,常常會出現卡死的狀態。幸運的是國內CNode社區的@fire9同窗利用空餘時間搭建了一個鏡像的NPM資源庫,服務器架設在日本,能夠繞過某些沒必要要的網絡問題。你能夠經過如下這條命令來安裝第三方庫:網絡
npm --registry "http://npm.hacknodejs.com/" install underscore
若是你想將它設爲默認的資源庫,運行下面這條命令便可:curl
npm config set registry 「http://npm.hacknodejs.com/」測試
設置以後每次安裝時就能夠不用帶上—registry參數。值得一提的是還有另外一個鏡像可用,該鏡像地址是http://registry.npmjs.vitecho.com,如需使用,替換上面兩行命令的地址便可。fetch
實際實踐結果以下:ui
輸入安裝NPM的命令以後 有點慢。。哈
tar=/bin/tar version: tar (GNU tar) 1.15.1 fetching: http://registry.npmjs.org/npm/-/npm-1.0.106.tgz 0.6.3 1.0.106 cleanup prefix=/usr/local All clean! /usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm-g -> /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm_g -> /usr/local/lib/node_modules/npm/bin/npm-cli.js npm@1.0.106 /usr/local/lib/node_modules/npm It worked
安裝完成 輸入 NPM 測試 結果以下
[root@YX-MID node-v0.6.3]# npm Usage: npm <command> where <command> is one of: adduser, apihelp, author, bin, bugs, c, cache, completion, config, deprecate, docs, edit, explore, faq, find, get, help, help-search, home, i, info, init, install, la, link, list, ll, ln, ls, outdated, owner, pack, prefix, prune, publish, r, rb, rebuild, remove, restart, rm, root, run-script, s, se, search, set, show, star, start, stop, submodule, tag, test, un, uninstall, unlink, unpublish, unstar, up, update, version, view, whoami npm <cmd> -h quick help on <cmd> npm -l display full usage info npm faq commonly asked questions npm help <term> search for help on <term> npm help npm involved overview Specify configs in the ini-formatted file: /root/.npmrc or on the command line via: npm <command> --key value Config info can be viewed via: npm help config