LoginSignup
2

More than 1 year has passed since last update.

npm で ECONNREFUSEDとか言われた

Last updated at Posted at 2022-01-28

症状

npm ERR! code ECONNREFUSED
npm ERR! syscall connect
npm ERR! errno ECONNREFUSED
npm ERR! FetchError: request to http://registry.npmjs.org/typescript failed, reason: connect ECONNREFUSED 2606:4700::6810:1b23:80
npm ERR!     at ClientRequest.<anonymous> (/usr/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:110:14)
npm ERR!     at ClientRequest.emit (node:events:390:28)
npm ERR!     at onerror (/usr/lib/node_modules/npm/node_modules/agent-base/dist/src/index.js:117:21)
npm ERR!     at callbackError (/usr/lib/node_modules/npm/node_modules/agent-base/dist/src/index.js:136:17)
npm ERR!     at processTicksAndRejections (node:internal/process/task_queues:96:5)
npm ERR!  FetchError: request to http://registry.npmjs.org/typescript failed, reason: connect ECONNREFUSED 2606:4700::6810:1b23:80
npm ERR!     at ClientRequest.<anonymous> (/usr/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:110:14)
npm ERR!     at ClientRequest.emit (node:events:390:28)
npm ERR!     at onerror (/usr/lib/node_modules/npm/node_modules/agent-base/dist/src/index.js:117:21)
npm ERR!     at callbackError (/usr/lib/node_modules/npm/node_modules/agent-base/dist/src/index.js:136:17)
npm ERR!     at processTicksAndRejections (node:internal/process/task_queues:96:5) {
npm ERR!   code: 'ECONNREFUSED',
npm ERR!   errno: 'ECONNREFUSED',
npm ERR!   syscall: 'connect',
npm ERR!   address: '2606:4700::6810:1b23',
npm ERR!   port: 80,
npm ERR!   type: 'system',
npm ERR!   requiredBy: '.'
npm ERR! }
npm ERR! 
npm ERR! If you are behind a proxy, please make sure that the
npm ERR! 'proxy' config is set properly.  See: 'npm help config'

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ahoxa/.npm/_logs/2022-01-26T01_33_37_064Z-debug-0.log

npm install 全般で起こった

原因を探る

  • ping registry.npmjs.orgは返ってこなかった
  • ブラウザだと返ってくる

→ いろんな人に相談した結果、ブラウザで帰ってくるのはブラウザがいい感じに通れる場所まで引っ張っていってくれるけど、コマンドからだと自動的に引っ張ってきてくれないのでは?という考察

原因を探る(2)

curl -v registry.npmjs.orgを叩いてみる

*   Trying 2606:4700::6810:1b23:80...
* connect to 2606:4700::6810:1b23 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1623:80...
* connect to 2606:4700::6810:1623 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1a23:80...
* connect to 2606:4700::6810:1a23 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1323:80...
* connect to 2606:4700::6810:1323 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1723:80...
* connect to 2606:4700::6810:1723 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1423:80...
* connect to 2606:4700::6810:1423 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1923:80...
* connect to 2606:4700::6810:1923 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1223:80...
* connect to 2606:4700::6810:1223 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1823:80...
* connect to 2606:4700::6810:1823 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1123:80...
* connect to 2606:4700::6810:1123 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1523:80...
* connect to 2606:4700::6810:1523 port 80 failed: 接続を拒否されました
*   Trying 2606:4700::6810:1023:80...
* connect to 2606:4700::6810:1023 port 80 failed: 接続を拒否されました
*   Trying 104.16.20.35:80...
* Connected to registry.npmjs.org (104.16.20.35) port 80 (#0)
> GET / HTTP/1.1
> Host: registry.npmjs.org
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Date: Fri, 28 Jan 2022 03:30:42 GMT
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 0
< Connection: keep-alive
< Location: https://registry.npmjs.org/
< Server: cloudflare
< CF-RAY: 6d4724c668708d24-KIX
< 
* Connection #0 to host registry.npmjs.org left intact

ドメイン名はIPアドレスというコンピュータがわかるサーバの住所に変換されるのだが、IPv6アドレスだとどうやら接続が拒否されている様子(原因はわからない)
ブラウザで接続して正常に返ってきたのは、v6アドレスで拒否された時に自動でv4に切り替えてアクセスしているから、のよう(詳細は不明)

解決法

今回はIPv6で接続できるようにするのが問題ではなく、npm installができるようになればいいので、v4アドレスへの接続を優先すればいい

というわけでIPアドレスとドメイン名を紐付けているらしい/etc/hostsに記述

104.16.20.35 registry.npmjs.org
104.16.20.35 registry.npmjs.org

変更後、再びcurl -v registry.npmjs.orgを叩く

*   Trying 104.16.19.35:80...
* Connected to registry.npmjs.org (104.16.19.35) port 80 (#0)
> GET / HTTP/1.1
> Host: registry.npmjs.org
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 301 Moved Permanently
< Date: Fri, 28 Jan 2022 03:36:10 GMT
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 0
< Connection: keep-alive
< Location: https://registry.npmjs.org/
< Server: cloudflare
< CF-RAY: 6d472cc909040ab2-KIX
< 
* Connection #0 to host registry.npmjs.org left intact

接続拒否等が出ず正常に動作

追記

yarnでも同様に対処しました

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2