虽然node.js在0.6版本开始就把npm包含在发行版中了,但我在Ubuntu上安装的v0.12.7版本却无法使用npm,不知是何原因,所以把npm重新单独安装了一遍,因为在Ubuntu下,所以可以使用apt-get进行安装:
$sudo apt-get install npm
查看npm被安装到哪里:
$whereis npm
设置成全局:
$ln -s [文件位置/npm] /usr/local/bin/npm
安装完成后,可使用 $npm -v 命令查看版本,若显示版本号,则安装成功。
然后可以用npm安装其他包了,但我这里出现了如下错误,安装的是supervisor这个包:
root@ybs-HVM-domU:/usr/bin# sudo npm install -g supervisor
npm http GET https://registry.npmjs.org/supervisor
npm ERR! Error: failed to fetch from registry: supervisor
npm ERR! at /usr/share/npm/lib/utils/npm-registry-client/get.js:139:12
npm ERR! at cb (/usr/share/npm/lib/utils/npm-registry-client/request.js:31:9)
npm ERR! at Request._callback (/usr/share/npm/lib/utils/npm-registry-client/request.js:136:18)
npm ERR! at Request.callback (/usr/lib/nodejs/request/main.js:119:22)
npm ERR! at Request.<anonymous> (/usr/lib/nodejs/request/main.js:212:58)
npm ERR! at Request.emit (events.js:88:20)
npm ERR! at ClientRequest.<anonymous> (/usr/lib/nodejs/request/main.js:412:12)
npm ERR! at ClientRequest.g (events.js:156:14)
npm ERR! at ClientRequest.emit (events.js:67:17)
npm ERR! at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1256:7)
npm ERR! You may report this log at:
npm ERR! <http://bugs.debian.org/npm>
npm ERR! or use
npm ERR! reportbug --attach /usr/bin/npm-debug.log npm
npm ERR!
npm ERR! System Linux 3.5.0-17-generic
npm ERR! command "/usr/bin/nodejs" "/usr/local/bin/npm" "install" "-g" "supervisor"
npm ERR! cwd /usr/bin
npm ERR! node -v v0.6.19
npm ERR! npm -v 1.1.4
npm ERR! message failed to fetch from registry: supervisor
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /usr/bin/npm-debug.log
npm not ok
找到了解决方法,执行如下命令:
$npm config set registry http://registry.npmjs.org/
后续发现用npm安装程序还是不好用,提示版本npm与node版本不兼容,因为npm是通过apt-get安装的0.6.19版本过老,与在node.js官网下载的最新版本v0.12.7的node版本相差太大,然后在node.js的官网上发现了npm的安装命令:
curl -L https://npmjs.org/install.sh | sh
使用此命令安装成功。