就像NPM的官网(http://npmjs.org/)上介绍的那样,安装NPM仅仅是一行命令的事情:
curl http://npmjs.org/install.sh | sh
这里详解一下这句命令的意思,curl http://npmjs.org/install.sh是通过curl命令获取这个安装shell脚本,按后通过管道符| 将获取的脚本交由sh命令来执行。这里如果没有权限会安装不成功,需要加上sudo来确保权限:
curl http://npmjs.org/install.sh | sudo sh
安装成功后执行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安装第三方包的过程。
npm install underscore
返回:
[email protected] ./node_modules/underscore
由于一些特殊的网络环境,直接通过npm install命令安装第三方库的时候,经常会出现卡死的状态。幸运的是国内CNode社区的@fire9同学利用空余时间搭建了一个镜像的NPM资源库,服务器架设在日本,可以绕过某些不必要的网络问题。你可以通过以下这条命令来安装第三方库:
npm --registry "http://npm.hacknodejs.com/" install underscore
如果你想将它设为默认的资源库,运行下面这条命令即可:
npm config set registry “http://npm.hacknodejs.com/”
设置之后每次安装时就可以不用带上—registry参数。值得一提的是还有另一个镜像可用,该镜像地址是http://registry.npmjs.vitecho.com,如需使用,替换上面两行命令的地址即可。
实际实践结果如下:
输入安装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 [email protected] /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