日期:2014-05-16  浏览次数:20554 次

linux npm 安装

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

返回:

underscore@1.2.2 ./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安装包。安裝好之後會自動被安裝到 /usr/local/bin 目錄下,而相依的函式庫也會自動安裝到 /usr/local/lib/node 目錄下,實在是非常方便。