NPM Registry: What If I Cannot Reach It?

2021-02-01
Header: npm-reg

This article is a bit old and the content may be outdated, so please refer to it with caution and remember to check the latest official materials (such as documentation, etc.)

The story of mirrors, ipv6, and yarn 2

Above image background from https://travelandleisureindia.in/great-wall-of-china-visitors-cap/

Well, you might say, npm config set registry xxx does the trick. I know how to set npm registry mirror of course. I just have another story to tell.

Registries are Shown in Lock File #

Npm or yarn classic, no matter which you are using, recommends you to commit the lock file (at least in several situations) and writes registry into lock file. What if you want to speed up development using one registry while speeding up CI with another registry, due to differrent network environments? What if the registry committed in version control is simply not reachable by others, which cannot be easily solved without deleting lock file? For more situations, see yarnpkg/yarn/issues/3330.

Say Hello to Yarn 2 #

Although PnP mode introducedn in yarn 2 has many capability issues, you can simply turn it of by setting nodeLinker: node-modules in .yarnrc.yml. Enjoy registry agnostic lock file... to some degree. https://registry.npm.taobao.org is not the case, because it doesn't follow the standard url patterns from the npm registry. See https://github.com/yarnpkg/berry/issues/2192#issuecomment-735833055 for the explanation. But good news, https://repo.huaweicloud.com/repository/npm/ works great.

Why Registry Works Fine in Browser but Fails in Terminal? #

This is a little bit confusing. I pinged registry.npmjs.org and found it was an ipv6 address. Then I tried to ping resolved ipv4 address (dig registry.npmjs.org A) but all of them are timed out. Maybe both npm and yarn do not support ipv6, in year 2021?

Nico Schottelius' blog post The Nodejs in IPv6 only networks problem is to the point. It's basically a NodeJS bug (nodejs/node/pull/31567). NodeJS reorders the DNS result so that IPv4 addresses come before IPv6 addresses by default, making it unfriendly to ipv6 network environments.

Workaround #

Set hosts to an ipv6 address to overwrite DNS result.

👍
3
Leave your comments and reactions on GitHub