安装grunt时,NodeJS NPM代理出现错误

20

当我尝试通过npm安装grunt时,出现以下错误:

C:\Program Files\nodejs\node_modules\npm>npm install -g grunt
npm ERR! network connect ETIMEDOUT
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "-g" "grunt"
npm ERR! cwd C:\Program Files\nodejs\node_modules\npm
npm ERR! node -v v0.10.33
npm ERR! npm -v 1.4.28
npm ERR! syscall connect
npm ERR! code ETIMEDOUT
npm ERR! errno ETIMEDOUT
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Program Files\nodejs\node_modules\npm\npm-debug.log
npm ERR! not ok code 0

我正在使用企业代理配置,我相信我遇到这个错误是因为代理配置的原因。为了成功安装grunt,我需要做出哪些修改?

11个回答

23

您需要配置npm配置文件,可以在终端中完成:

npm config set proxy http://proxy.company.com:8080

npm config set https-proxy http://proxy.company.com:8080

你的错误日志建议查看“npm help config”,所以我认为问题就出在那里。

如果需要更多解释,请查看此博客文章(还有很多其他文章)

祝你好运!


11
如果你在"Windows"域中使用代理的话,需要将域名添加到代理URL中:
npm config set proxy http://domain%5Cuser:password@proxy.company.com:8080
npm config set https-proxy http://domain%5Cuser:password@proxy.company.com:8080

您需要将反斜杠编码为http uri字符串:%5C

如果您的用户名或密码中有特殊字符,则有必要对这些字符进行编码。请记住,这些关键信息以明文形式存储在npm配置文件(%HOME%\.npmrc)中。可能还需要将npm注册表指向http源:

npm config set registry "http://registry.npmjs.org"
npm config set strict-ssl false

7

这个配置对我有效。你需要检查你的http和https端口(通常分别为80和443),但在我的情况下,我将两者都设置为了80端口。

npm config set proxy http://user:password@proxy.url.com:80

npm config set https-proxy http://user:password@proxy.url.com:80

您可以使用“get”命令检查代理设置。
npm config get proxy

npm config get https-proxy

6

我以前遇到过同样的问题,因为我错误地玩弄了代理配置,就像这样:

npm config set proxy http://localhost:8080/ npm config set https-proxy http://localhost:8080/ npm config set strict-ssl false

这使得npm客户端尝试访问localhost:8080来拉取模块,而不是正确的互联网终点。

所以,在经历了几天的挫折后,我找到了这个链接 https://docs.npmjs.com/cli/config

它告诉你要运行以下命令:

 npm config edit

我打开了一个文件,在那个文件里我删除了之前添加的三行代码,是的,一切都正常工作。希望这有所帮助。ALH


就是这样! 我运行了 npm config edit 并发现其中有一个奇怪的IP,然后我删除了它,现在问题解决了。 - esejuanb

1

1

0

你可以在命令提示符中运行这个

npm config set <key> <value> [-g|--global]
npm config get <key>
npm config delete <key>
npm config list
npm config edit
npm get <key>
npm set <key> <value> [-g|--global]

npmrc.txt 将运行,您可以更改链接和端口

 proxy =http://proxy.company.com:8080

 https-proxy = http://proxy.company.com:8080

参考资料


0

0

0

错误是由于在目录C://Users/<username>/中的.npmrc文件中设置的配置引起的。

在记事本中打开.npmrc文件并删除其中设置的代理。您只需要将ms build版本和ssl设置为false,如this screenshot所示。

这将解决问题。现在npm install将完美地工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接