メインコンテンツまでスキップ

Linux Mint で npm install すると ECONNREFUSED となる際の対処法

以前 Raspberry Pi で起きたことが Linux Mint でも起きた。

$ npm install --global yarn

をすると次のエラーが出た。

npm ERR! code ECONNREFUSED
npm ERR! syscall connect
npm ERR! errno ECONNREFUSED
npm ERR! FetchError: request to https://registry.npmjs.org/yarn failed, reason: connect ECONNREFUSED 2606:4700::6810:1a23:443
npm ERR! at ClientRequest.<anonymous> (/usr/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:130:14)
npm ERR! at ClientRequest.emit (node:events:513:28)
npm ERR! at TLSSocket.socketErrorListener (node:_http_client:494:9)
npm ERR! at TLSSocket.emit (node:events:525:35)
npm ERR! at emitErrorNT (node:internal/streams/destroy:151:8)
npm ERR! at emitErrorCloseNT (node:internal/streams/destroy:116:3)
npm ERR! at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
npm ERR! FetchError: request to https://registry.npmjs.org/yarn failed, reason: connect ECONNREFUSED 2606:4700::6810:1a23:443
npm ERR! at ClientRequest.<anonymous> (/usr/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:130:14)
npm ERR! at ClientRequest.emit (node:events:513:28)
npm ERR! at TLSSocket.socketErrorListener (node:_http_client:494:9)
npm ERR! at TLSSocket.emit (node:events:525:35)
npm ERR! at emitErrorNT (node:internal/streams/destroy:151:8)
npm ERR! at emitErrorCloseNT (node:internal/streams/destroy:116:3)
npm ERR! at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
npm ERR! code: 'ECONNREFUSED',
npm ERR! errno: 'ECONNREFUSED',
npm ERR! syscall: 'connect',
npm ERR! address: '2606:4700::6810:1a23',
npm ERR! port: 443,
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'

Raspberry Pi のときは IPv6 を無効にしたけど、どうも /etc/hosts に IPv4 のアドレスを追加すると解決するらしいことがわかった。というわけでディグった。

$ dig registry.npmjs.org
; <<>> DiG 9.18.1-1ubuntu1.1-Ubuntu <<>> registry.npmjs.org
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17987
;; flags: qr rd ra; QUERY: 1, ANSWER: 12, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;registry.npmjs.org. IN A

;; ANSWER SECTION:
registry.npmjs.org. 23 IN A 104.16.26.35
registry.npmjs.org. 23 IN A 104.16.22.35
registry.npmjs.org. 23 IN A 104.16.25.35
registry.npmjs.org. 23 IN A 104.16.23.35
registry.npmjs.org. 23 IN A 104.16.16.35
registry.npmjs.org. 23 IN A 104.16.24.35
registry.npmjs.org. 23 IN A 104.16.19.35
registry.npmjs.org. 23 IN A 104.16.20.35
registry.npmjs.org. 23 IN A 104.16.21.35
registry.npmjs.org. 23 IN A 104.16.17.35
registry.npmjs.org. 23 IN A 104.16.27.35
registry.npmjs.org. 23 IN A 104.16.18.35

;; Query time: 3 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Sun Sep 18 17:49:36 JST 2022
;; MSG SIZE rcvd: 239

これらを /etc/hosts に追加したら動いた。

続いて yarn install したら似たようなエラーが出たので、同様にディグった。

$ dig registry.yarnpkg.com
; <<>> DiG 9.18.1-1ubuntu1.1-Ubuntu <<>> registry.yarnpkg.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23366
;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 13

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;registry.yarnpkg.com. IN A

;; ANSWER SECTION:
registry.yarnpkg.com. 123 IN CNAME yarn.npmjs.org.
yarn.npmjs.org. 10 IN A 104.16.16.35
yarn.npmjs.org. 10 IN A 104.16.22.35
yarn.npmjs.org. 10 IN A 104.16.20.35
yarn.npmjs.org. 10 IN A 104.16.24.35
yarn.npmjs.org. 10 IN A 104.16.26.35
yarn.npmjs.org. 10 IN A 104.16.25.35
yarn.npmjs.org. 10 IN A 104.16.21.35
yarn.npmjs.org. 10 IN A 104.16.18.35
yarn.npmjs.org. 10 IN A 104.16.27.35
yarn.npmjs.org. 10 IN A 104.16.23.35
yarn.npmjs.org. 10 IN A 104.16.17.35
yarn.npmjs.org. 10 IN A 104.16.19.35

;; ADDITIONAL SECTION:
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1823
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1623
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1923
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1323
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1723
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1423
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1523
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1a23
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1223
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1123
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1b23
yarn.npmjs.org. 123 IN AAAA 2606:4700::6810:1023

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Sun Sep 18 18:27:30 JST 2022
;; MSG SIZE rcvd: 605

yarn は一度 CNAME を挟んでいるので、 /etc/hosts では 104.16.16.35 registry.yarnpkg.com の形で追加した。

ところで Linux Mint で www.google.com といったアドレスや IPv6 アドレスに ping しても通らない。 Windows だと ping www.google.com すると IPv4 のアドレスに ping されて、 ping 2001:4860:4860::8888 すると「要求がタイムアウトしました。」と出る。なにか設定が必要なのだろうか。後日調べるとしよう。

参考にしたサイト