Tech is political: The people under attack in Palestine 🇵🇸, Iran 🇮🇷, and Lebanon 🇱🇧 are people like us. They’re our brothers and sisters, too. Read up on their history, scrutinize what you’re told, and demand that they be respected and included. Hide

Frontend Dogma

npm Proxy for Installing Dependencies

on , tagged , , , (share this post, e.g., on Mastodon or on Bluesky)

Even though I was already using the Taobao npm mirror, some packages still download from overseas addresses—for example, PhantomJS’s download URL is something like https://bitbucket/xxxx—so without a proxy it still gets stuck.

We can use bitinn/kneesocks to support npm socket proxies; as I understand it, its role is to generate a local HTTP proxy.

Install:

$ npm install kneesocks --production -g

Create a local proxy:

$ kneesocks PORT1 PORT2

To explain: PORT1 is an unused new port for kneesocks to listen on, and PORT2 is an existing local proxy—for example, the local port of the Shadowsocks (ss) we already have installed.

For example: kneesocks 1082 1080, where 1080 is the local ss port.

Configure the npm proxy:

$ npm config set proxy http://127.0.0.1:1082
$ npm config set https-proxy http://127.0.0.1:1082

[Translator’s note: The original used http instead of proxy.]

Note: Use the kneesocks port.

Then you can run npm install normally.

If you later want to remove the proxy:

$ npm config delete proxy
$ npm config delete https-proxy

To debug the proxy, use the following command:

$ DEBUG=proxy kneesocks PORT1 PORT2

(This post is a machine-made, human-reviewed, and authorized translation of overtrue.me/­npm-proxy/­.)