就像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