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

node.js npm 设置http代理
node.js 的npm命令是node.js的包管理工具,安装外部模块时只需npm install packagname 即可,但是对于上网时使用代理的却安装不了:

F:\node.js\learn>npm install formidable
npm http GET http://registry.npmjs.vitecho.com/formidable
npm ERR! Error: failed to fetch from registry: formidable
npm ERR!     at C:\Program Files\nodejs\node_modules\npm\lib\utils\npm-registry-client\get.js:139:12
npm ERR!     at cb (C:\Program Files\nodejs\node_modules\npm\lib\utils\npm-registry-client\request.js:32:9)
npm ERR!     at Request._callback (C:\Program Files\nodejs\node_modules\npm\lib\utils\npm-registry-client\request.js:137:18)
npm ERR!     at Request.callback (C:\Program Files\nodejs\node_modules\npm\node_modules\request\main.js:104:22)
npm ERR!     at Request.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\request\main.js:181:58)
npm ERR!     at Request.emit (events.js:88:20)
npm ERR!     at ClientRequest.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\request\main.js:178:10)
npm ERR!     at ClientRequest.emit (events.js:67:17)
npm ERR!     at Socket.<anonymous> (http.js:1127:11)
npm ERR!     at Socket.emit (events.js:67:17)
npm ERR! Report this *entire* log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>
npm ERR!
npm ERR! System Windows_NT 5.1.2600
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "formidable"
npm ERR! cwd F:\node.js\learn
npm ERR! node -v v0.6.7
npm ERR! npm -v 1.1.0-beta-10


对于此种情况只需设置一下npm的环境变量就可以了,只可惜官方说明不是很详细,我在github上问了之后才得到答案。运行如下命令:
npm config set proxy=http://192.168.1.1:8080
就可以了,但是看官方的文档中明明有http_proxy、https_proxy两个变量,设置了却不起作用。