如何正确挂代理解决执行 npm install 出现连接错误的问题?

2020-02-05 09:17:30 +08:00
 fenglala

我参考教程 https://www.electronjs.org/docs/tutorial/first-app

mkdir, cd, npm init都没有问题

~/.bashrc 内容:

export HTTP_PROXY=http://127.0.0.1:8123/
export HTTPS_PROXY=http://127.0.0.1:8123/
export http_proxy=http://127.0.0.1:8123/
export https_proxy=http://127.0.0.1:8123/

~/.npmrc 内容:

proxy=http://127.0.0.1:8123/
http-proxy=http://127.0.0.1:8123/
https-proxy=http://127.0.0.1:8123/
noproxy=localhost,127.0.0.1,192.168.,10.
strict-ssl=false

执行npm install --save-dev electron时出现连接错误如下:

lala@ubu:~/projects/electron/my-app 17:20:34
$ npm install --save-dev electron

> core-js@3.6.4 postinstall /home/lala/projects/electron/my-app/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 
> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)


> electron@8.0.0 postinstall /home/lala/projects/electron/my-app/node_modules/electron
> node install.js

(node:5950) UnhandledPromiseRejectionWarning: RequestError: connect ETIMEDOUT 13.250.177.223:443
    at ClientRequest.<anonymous> (/home/lala/projects/electron/my-app/node_modules/got/source/request-as-event-emitter.js:178:14)
    at Object.onceWrapper (events.js:313:26)
    at ClientRequest.emit (events.js:228:7)
    at ClientRequest.origin.emit (/home/lala/projects/electron/my-app/node_modules/@szmarczak/http-timer/source/index.js:37:11)
    at TLSSocket.socketErrorListener (_http_client.js:406:9)
    at TLSSocket.emit (events.js:223:5)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)
(node:5950) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5950) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-app@1.0.0 No description
npm WARN my-app@1.0.0 No repository field.

+ electron@8.0.0
added 85 packages from 91 contributors and audited 102 packages in 87.419s

2 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

家里的河南联通不能直接访问 github.com(13.250.177.223)。但是代理已经配置成上边那样子了,没效果。

查了一圈 Google 和 stackoverflow 没找到,上 stackoverflow 提问目前为止还没人理我(请不要笑我的烂英语:)

https://stackoverflow.com/questions/60054531/how-can-i-solve-the-connection-problem-during-npm-install-behind-a-proxy

请问各位如何才能解决这个连接问题呢?

36497 次点击
所在节点    Electron
30 条回复
fenglala
2020-02-05 10:36:32 +08:00
@learnshare 不行,还是卡在这
fenglala
2020-02-05 10:40:12 +08:00
@francis59 不行,我把 bashrc 里的全都删了,npmrc 里边剩一行 registry=http 淘宝链接。。。。gistry.npm.taobao.org V2EX 说我每个回复都有 url 看起来像发广告的。。。。
执行的时候前边前边明显快多了说明淘宝的源用上了,但是还是卡在同样的地方
lala@ubu:~/projects/electron 10:35:53
$ npm install --save-dev electron
。。。。。。。。省略省略。。。。。。

> electron@8.0.0 postinstall /home/lala/projects/electron/node_modules/electron
> node install.js
learnshare
2020-02-05 10:44:48 +08:00
@fenglala npmrc 里边剩一行 registry=http
这个也删掉,保持操作环境干干净净,不要换源

就两个思路:
1. ALL_PROXY 全走代理(没 host )
2. host github,其他的也不走代理
francis59
2020-02-05 10:45:32 +08:00
@fenglala #22
不需要手动改 npmrc 啊,先把之前 npmrc 里面的代理相关的东西删掉( registry 那个也删掉),
然后执行以下命令安装 cnpm:
npm install -g cnpm --registry=https://registry.npm.taobao.org

安装完之后,就可以用 cnpm 代替 npm 了:
cnpm install --save-dev electron
fenglala
2020-02-05 10:49:19 +08:00
@learnshare @francis59 你俩的方法我分开尝试的,没有混淆。
用 @learnshare 的方法 1 ALL_PROXY 没用
用 @learnshare 的方法 2 host github 还是没用
用 @francis59 的方法 3 换成淘宝的源,确实换成淘宝的源了但是这玩意还是要连接 github 连不上

现在我打算整个跟 win 下的 sstap 类似的那种东西,把整个系统的所有流量都走代理,找到了个项目现在试试

https://github.com/semigodking/redsocks
fenglala
2020-02-05 10:50:21 +08:00
@learnshare @francis59 谢谢各位的回复!
learnshare
2020-02-05 10:51:13 +08:00
@fenglala
>(可以使用 17ce DNS 功能查一下有哪些你能访问到的 IP )
fenglala
2020-02-05 11:23:24 +08:00
@learnshare 我找到了一个百分百不丢包的 ip 140.82.114.3 刚才重启换上之后,确保直连能打开 github,然后还是卡在这一步。刚才回复不了,v2ex 说我回复太多不正常,让我 1800 秒之后再回复
otakustay
2020-02-05 11:33:50 +08:00
.npmrc 里加
electron_mirror=https://cdn.npm.taobao.org/dist/electron/
这是适用 electron 7.x 的好像,6.x 似乎是另一个地址
fenglala
2020-02-05 11:47:42 +08:00
@otakustay 问题解决了!感谢!
解决方法:
1. bashrc 里边不加任何跟代理有关的环境变量
2. npmrc 里边只加两行
registry=https://registry 点 npm 点 taobao 点 org
electron_mirror=https://cdn 点 npm 点 taobao 点 org/dist/electron/
3. hosts 将 github 执行可访问的 ip,我这是 140.82.114.3 (不确定这个有没有作用)
然后就一切按照教程执行就没任何问题!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/642210

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX