如何正确挂代理解决执行 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

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

36466 次点击
所在节点    Electron
30 条回复
learnshare
2020-02-05 09:19:28 +08:00
ALL_PROXY=http://127.0.0.1:xxxx npm install
fenglala
2020-02-05 09:21:18 +08:00
@learnshare 谢谢提议,我试一下
fenglala
2020-02-05 09:22:38 +08:00
@learnshare 还是不行,错误一样

```
lala@ubu:~/projects/electron/my-app 09:20:18
$ ALL_PROXY=http://127.0.0.1:8123 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:7836) UnhandledPromiseRejectionWarning: RequestError: connect ETIMEDOUT 13.229.188.59: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:7836) 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:7836) [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 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 80.372s

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

found 0 vulnerabilities

```
learnshare
2020-02-05 09:28:18 +08:00
fenglala
2020-02-05 09:30:15 +08:00
@learnshare 好的我再试一下,多谢!完事了回复你。
zjq123
2020-02-05 09:32:03 +08:00
什么 http 代理软件?
francis59
2020-02-05 09:38:06 +08:00
试试淘宝 cnpm ? https://npm.taobao.org/
fenglala
2020-02-05 09:48:11 +08:00
@zjq123 用 polipo 将 socks5 代理转换成的 http 代理
fenglala
2020-02-05 09:48:48 +08:00
@francis59 我试一下,多谢!搞完回复
fenglala
2020-02-05 09:53:53 +08:00
@learnshare 多谢你的建议!

现在我的 bashrc 和 npmrc 如下

lala@ubu:~/projects/electron/my-app 09:49:34
$ cat ~/.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
global.GLOBAL_AGENT=true
global.GLOBAL_AGENT.HTTP_PROXY=http://127.0.0.1:8123/
global.GLOBAL_AGENT.HTTPS_PROXY=http://127.0.0.1:8123/
lala@ubu:~/projects/electron/my-app 09:49:41
$ tail -n 10 ~/.bashrc

export ANDROID_HOME=/home/lala/AndroidSDK
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/
export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8123/
export GLOBAL_AGENT_HTTPS_PROXY=http://127.0.0.1:8123/
export ELECTRON_GET_USE_PROXY=true
export ELECTRON_MIRROR="https://cdn.npm.taobao.org/dist/electron/"

但是还是卡在这个地方。但是另一方面这个 electron 依赖看起来已经下载下来了
lala@ubu:~/projects/electron/my-app/node_modules/electron 09:50:52
$ tree .
.
├── cli.js
├── electron.d.ts
├── index.js
├── install.js
├── LICENSE
├── package.json
└── README.md

0 directories, 7 files
lala@ubu:~/projects/electron/my-app/node_modules/electron 09:51:10
$ wc electron.d.ts
13622 61347 541692 electron.d.ts

所以要解决的这个问题应该不是下载依赖,而是如何解决连接问题。
kumiko
2020-02-05 10:02:33 +08:00
我是执行 npx 命令的时候网络经常抽风,也是代理都没用,估计是未成功
learnshare
2020-02-05 10:10:23 +08:00
@fenglala
1. 去掉如下内容
$ cat ~/.npmrc
proxy=http://127.0.0.1:8123/
http-proxy=http://127.0.0.1:8123/
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/
export http_proxy=http://127.0.0.1:8123/
export https_proxy=http://127.0.0.1:8123/
export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8123/
export GLOBAL_AGENT_HTTPS_PROXY=http://127.0.0.1:8123/
export ELECTRON_GET_USE_PROXY=true
export ELECTRON_MIRROR="https://cdn.npm.taobao.org/dist/electron/"

2. 然后删除 node_modules
3. 使用 ALL_PROXY=http://127.0.0.1:8123 npm install --loglevel silly 重新安装

注意观察日志
fenglala
2020-02-05 10:11:40 +08:00
@kumiko 好多软件设置可选代理,但是好多软件的代理都不生效就很烦人
fenglala
2020-02-05 10:17:16 +08:00
@learnshare 现在指示删掉了 bashrc 里的那些,npmrc 里边还剩如下:
lala@ubu:~/projects/electron/my-app 10:13:56
$ cat ~/.npmrc
noproxy=localhost,127.0.0.1,192.168.,10.
strict-ssl=false
global.GLOBAL_AGENT=true
global.GLOBAL_AGENT.HTTP_PROXY=http://127.0.0.1:8123/
global.GLOBAL_AGENT.HTTPS_PROXY=http://127.0.0.1:8123/
registry=https://registry.npm.taobao.org

执行 ALL_PROXY=http://127.0.0.1:8123 npm install --loglevel silly 的时候没有任何问题,一切正常。

但是执行 ALL_PROXY=http://127.0.0.1:8123 npm install --loglevel silly --save-dev electron 还是报那个错,最终结果显示出来还是安装完成,中间日志如下:

npm sill postinstall got@9.6.0
npm info lifecycle got@9.6.0~postinstall: got@9.6.0
npm sill postinstall @electron/get@1.7.2
npm info lifecycle @electron/get@1.7.2~postinstall: @electron/get@1.7.2
npm sill postinstall yauzl@2.4.1
npm info lifecycle yauzl@2.4.1~postinstall: yauzl@2.4.1
npm sill postinstall extract-zip@1.6.7
npm info lifecycle extract-zip@1.6.7~postinstall: extract-zip@1.6.7
npm sill postinstall electron@8.0.0
npm info lifecycle electron@8.0.0~postinstall: electron@8.0.0

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

(node:11541) 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:11541) 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:11541) [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 verb lifecycle electron@8.0.0~postinstall: unsafe-perm in lifecycle true
npm verb lifecycle electron@8.0.0~postinstall: PATH: /usr/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/home/lala/projects/electron/my-app/node_modules/electron/node_modules/.bin:/home/lala/projects/electron/my-app/node_modules/.bin:/home/lala/.local/bin:/home/lala/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
npm verb lifecycle electron@8.0.0~postinstall: CWD: /home/lala/projects/electron/my-app/node_modules/electron
npm sill lifecycle electron@8.0.0~postinstall: Args: [ '-c', 'node install.js' ]
npm sill lifecycle electron@8.0.0~postinstall: Returned: code: 0 signal: null
npm timing action:postinstall Completed in 31826ms
npm verb unlock done using /home/lala/.npm/_locks/staging-7ae29a0b4c2104a4.lock for /home/lala/projects/electron/my-app/node_modules/.staging
npm timing stage:executeActions Completed in 33034ms

最底部日志如下:

npm sill install saveToDependencies
npm verb saving [ { name: 'electron', spec: '^8.0.0', save: 'devDependencies' } ]
npm verb shrinkwrap skipping write for package.json because there were no changes.
npm info lifecycle undefined~preshrinkwrap: undefined
npm info lifecycle undefined~shrinkwrap: undefined
npm info lifecycle undefined~postshrinkwrap: undefined
npm WARN my-app@1.0.0 No description
npm WARN my-app@1.0.0 No repository field.

npm sill install printInstalled
+ electron@8.0.0
updated 57 packages in 37.468s

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

npm verb exit [ 0, true ]
npm timing npm Completed in 37893ms
npm info ok
fenglala
2020-02-05 10:20:52 +08:00
@learnshare 谢谢一直帮忙。他这是不是从头到尾设置的代理都没生效过啊,还是说安装依赖的时候代理生效了,就连接时候没走代理呢?
learnshare
2020-02-05 10:23:09 +08:00
@fenglala 13.250.177.223 应该是 github.com 被污染的地址,无法访问

我目前通过 host 访问 github.com (可以使用 17ce DNS 功能查一下有哪些你能访问到的 IP )

140.82.113.4 github.com

你可以配置 host 之后,使用 npm install --loglevel silly --save-dev electron 安装
fenglala
2020-02-05 10:23:13 +08:00
@francis59 试了淘宝 cnpm 也是卡在一样的地方
fenglala
2020-02-05 10:24:22 +08:00
@learnshare 我感觉不是被污染的地址,因为我是通过直接把 IP 敲到 chrome 地址栏,然后跳转到 github 的。我试一下你说的 hosts 的方法。
francis59
2020-02-05 10:26:11 +08:00
@fenglala #17 取消代理了吗? cnpm 是不需要代理的
fenglala
2020-02-05 10:30:14 +08:00
@learnshare 改完重启了,现在正在执行你说的那个命令,我也不确定这个丢包率能不能连接成功

lala@ubu:~/projects/electron/my-app 10:27:45
$ ping github.com
PING github.com (140.82.113.4) 56(84) bytes of data.
64 bytes from github.com (140.82.113.4): icmp_seq=2 ttl=49 time=262 ms
^C
--- github.com ping statistics ---
5 packets transmitted, 1 received, 80% packet loss, time 48ms
rtt min/avg/max/mdev = 262.334/262.334/262.334/0.000 ms
lala@ubu:~/projects/electron/my-app 10:28:01
$ ping github.com
PING github.com (140.82.113.4) 56(84) bytes of data.
64 bytes from github.com (140.82.113.4): icmp_seq=1 ttl=49 time=290 ms
64 bytes from github.com (140.82.113.4): icmp_seq=4 ttl=49 time=261 ms
64 bytes from github.com (140.82.113.4): icmp_seq=5 ttl=49 time=336 ms
64 bytes from github.com (140.82.113.4): icmp_seq=6 ttl=49 time=268 ms
64 bytes from github.com (140.82.113.4): icmp_seq=8 ttl=49 time=377 ms
64 bytes from github.com (140.82.113.4): icmp_seq=9 ttl=49 time=274 ms
^C
--- github.com ping statistics ---
9 packets transmitted, 6 received, 33.3333% packet loss, time 91ms
rtt min/avg/max/mdev = 261.054/300.962/377.263/41.984 ms

目前还是卡在
> electron@8.0.0 postinstall /home/lala/projects/electron/my-app/node_modules/electron
> node install.js
我再等一会再看看

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

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

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

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

© 2021 V2EX