Set the registry for NPM and Yarn

  1. Use a new registry
  2. Keep it intact
  3. Forward traffic to a SOCKS5 proxy

Use a new registry

For those developers who live in China, you may have the problem when installing packages with NPM or Yarn, it could take hours to get the installation done or even failed after a long time.

There are several ways to address this problems, the easiest way might be using a custom registry, for example the one provided by Taobao:

1
2
npm config set registry http://registry.npm.taobao.org
yarn config set registry https://registry.npm.taobao.org

This works, however, remember to restore the registry to its original one before publishing packages:

1
2
npm config set registry https://registry.npmjs.org
yarn config set registry https://registry.yarnpkg.com

Keep it intact

If you prefer not to change the default registry, you could use NPM behind a proxy server.

Run the following commands one by one in your favourite terminal:

1
2
3
4
npm config set strict-ssl false
npm config set https-proxy http://username:password@host:port
npm config set proxy http://username:password@host:port
npm config set registry http://registry.npmjs.org/

Suppose you have a proxy running on localhost and the port is 8118, then the format is:

1
2
npm config set https-proxy http://127.0.0.1:8118
npm config set proxy http://127.0.0.1:8118

Run the following commands to check your proxy settings:

1
2
npm config get proxy
npm config get https-proxy

Forward traffic to a SOCKS5 proxy

If you are running a socks5 proxy server rather than an http proxy server, you will have to forward all http traffic to it, because NPM has no native support for socks proxies. I would recommend Privoxy as it does a good job for it.

  1. Download and install Privoxy.
  2. Find its config.txt file in the directory where it was installed.
  3. Add this line to it, suppose your socks5 proxy is running on port 1080:
1
forward-socks5 / 127.0.0.1:1080 .
  1. Then you are able to use the HTTP or HTTPS proxy at 127.0.0.1:8118, as Privoxy listens 127.0.0.1 and the port 8118 by default.