如何正确挂代理解决执行 npm install 出现连接错误的问题? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fenglala
V2EX    Electron

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

  •  
  •   fenglala
    code4lala 2020-02-05 09:17:30 +08:00 39070 次点击
    这是一个创建于 2125 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我参考教程 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 > [email protected] 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 -) > [email protected] 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 [email protected] No description npm WARN [email protected] No repository field. + [email protected] 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

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

    30 条回复    2020-02-05 11:47:42 +08:00
    learnshare
        1
    learnshare  
       2020-02-05 09:19:28 +08:00
    ALL_PROXY=http://127.0.0.1:xxxx npm install
    fenglala
        2
    fenglala  
    OP
       2020-02-05 09:21:18 +08:00
    @learnshare 谢谢提议,我试一下
    fenglala
        3
    fenglala  
    OP
       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

    > [email protected] postnstall /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 -)


    > [email protected] 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 [email protected] No description
    npm WARN [email protected] No repository field.

    + [email protected]
    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

    ```
    fenglala
        5
    fenglala  
    OP
       2020-02-05 09:30:15 +08:00
    @learnshare 好的我再试一下,多谢!完事了回复你。
    zjq123
        6
    zjq123  
       2020-02-05 09:32:03 +08:00 via Android
    什么 http 代理软件?
    francis59
        7
    francis59  
       2020-02-05 09:38:06 +08:00
    试试淘宝 cnpm ? https://npm.taobao.org/
    fenglala
        8
    fenglala  
    OP
       2020-02-05 09:48:11 +08:00
    @zjq123 用 polipo 将 socks5 代理转换成的 http 代理
    fenglala
        9
    fenglala  
    OP
       2020-02-05 09:48:48 +08:00
    @francis59 我试一下,多谢!搞完回复
    fenglala
        10
    fenglala  
    OP
       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
        11
    kumiko  
       2020-02-05 10:02:33 +08:00 via Android
    我是执行 npx 命令的时候网络经常抽风,也是代理都没用,估计是未成功
    learnshare
        12
    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
        13
    fenglala  
    OP
       2020-02-05 10:11:40 +08:00
    @kumiko 好多软件设置可选代理,但是好多软件的代理都不生效就很烦人
    fenglala
        14
    fenglala  
    OP
       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 [email protected]
    npm info lifecycle [email protected]~postinstall: [email protected]
    npm sill postinstall @electron/[email protected]
    npm info lifecycle @electron/[email protected]~postinstall: @electron/[email protected]
    npm sill postinstall [email protected]
    npm info lifecycle [email protected]~postinstall: [email protected]
    npm sill postinstall [email protected]
    npm info lifecycle [email protected]~postinstall: [email protected]
    npm sill postinstall [email protected]
    npm info lifecycle [email protected]~postinstall: [email protected]

    > [email protected] 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 [email protected]~postinstall: unsafe-perm in lifecycle true
    npm verb lifecycle [email protected]~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 [email protected]~postinstall: CWD: /home/lala/projects/electron/my-app/node_modules/electron
    npm sill lifecycle [email protected]~postinstall: Args: [ '-c', 'node install.js' ]
    npm sill lifecycle [email protected]~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 [email protected] No description
    npm WARN [email protected] No repository field.

    npm sill install printInstalled
    + [email protected]
    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
        15
    fenglala  
    OP
       2020-02-05 10:20:52 +08:00
    @learnshare 谢谢一直帮忙。他这是不是从头到尾设置的代理都没生效过啊,还是说安装依赖的时候代理生效了,就连接时候没走代理呢?
    learnshare
        16
    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
        17
    fenglala  
    OP
       2020-02-05 10:23:13 +08:00
    @francis59 试了淘宝 cnpm 也是卡在一样的地方
    fenglala
        18
    fenglala  
    OP
       2020-02-05 10:24:22 +08:00
    @learnshare 我感觉不是被污染的地址,因为我是通过直接把 IP 敲到 chrome 地址栏,然后跳转到 github 的。我试一下你说的 hosts 的方法。
    francis59
        19
    francis59  
       2020-02-05 10:26:11 +08:00
    @fenglala #17 取消代理了吗? cnpm 是不需要代理的
    fenglala
        20
    fenglala  
    OP
       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

    目前还是卡在
    > [email protected] postinstall /home/lala/projects/electron/my-app/node_modules/electron
    > node install.js
    我再等一会再看看
    fenglala
        21
    fenglala  
    OP
       2020-02-05 10:36:32 +08:00
    @learnshare 不行,还是卡在这
    fenglala
        22
    fenglala  
    OP
       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
    。。。。。。。。省略省略。。。。。。

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

    就两个思路:
    1. ALL_PROXY 全走代理(没 host )
    2. host github,其他的也不走代理
    francis59
        24
    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
        25
    fenglala  
    OP
       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
        26
    fenglala  
    OP
       2020-02-05 10:50:21 +08:00
    @learnshare @francis59 谢谢各位的回复!
    learnshare
        27
    learnshare  
       2020-02-05 10:51:13 +08:00
    @fenglala
    >(可以使用 17ce DNS 功能查一下有哪些你能访问到的 IP )
    fenglala
        28
    fenglala  
    OP
       2020-02-05 11:23:24 +08:00
    @learnshare 我找到了一个百分百不丢包的 ip 140.82.114.3 刚才重启换上之后,确保直连能打开 github,然后还是卡在这一步。刚才回复不了,v2ex 说我回复太多不正常,让我 1800 秒之后再回复
    otakustay
        29
    otakustay  
       2020-02-05 11:33:50 +08:00   1
    .npmrc 里加
    electron_mirror=https://cdn.npm.taobao.org/dist/electron/
    这是适用 electron 7.x 的好像,6.x 似乎是另一个地址
    fenglala
        30
    fenglala  
    OP
       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 (不确定这个有没有作用)
    然后就一切按照教程执行就没任何问题!
    关于     帮助文档     自助推广系统     博客     API     FAQ    Solana     843 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 19:33 PVG 03:33 LAX 11:33 JFK 14:33
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86