Unix/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的命令之后 有点慢。。哈

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

转载于:https://my.oschina.net/zhangdapeng89/blog/52783

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值