Javascriptnpm

Guiding NPM Through a Firewall

npm doesn’t like to be fenced in by a corporate firewall. Too bad most npm installations live precisely there. The other day, I ran an npm install on the customer’s PC. The same command that finishes in 40 seconds on my machine took roughly a quarter of an hour. Plus several hours of troubleshooting. It was a Deja-Vue experience to me, having lived this nightmare several times before, so it’s high time to collect a short list of hints. BTW, if you want to contribute to this list of life-savers, please do so. Just leave a comment, thus giving me an opportunity to include it to the list.

Basic proxy settings

These two settings almost always do the trick:

npm config set https-proxy http://username:passwort@proxy.mycompany.com:8080
npm config set proxy http://username:passwort@proxy.mycompany.com:8080

Note that both the https-proxy and the proxy use the same URL. I don’t know if that’s always the case. But usually, it does.

If you don’t know the proxy settings, you often find them in the settings of the Internet Explorer. Otherwise, ask your IT department.

Including the domain name to the user name

Sometimes you also have to prefix the username with the domain name. If so, escape the backslash by %5C.

Using relaxed SSL settings

The next hint is a bit scary. I don’t know why it works at all. I guess is that it works if your company breaks the SSL connection in order to be able to check your traffic for viruses. That’s far from being uncommon, so it’s not a surprise that many npm installations consider the SSL connection suspicious. But that’s only guesswork. Be that as it may, try this setting if you still experience problems during an npm install:

npm set strict-ssl false

Switching off SSL altogether

The next hint is even scarier. You can ask npm to use the npm server address that doesn’t use SSL. Here we go:

npm config set registry=http://registry.npmjs.org/

Limiting the number of parallel request

Sometimes, the problem is that npm tries to download as many files as possible in parallel. By default, that’s 40 simultaneous connections. That, in turn, may lead to problems if the virus scanner on the proxy is slow. The result is a strange “Bad address” error – or no error at all, just a stalling npm download.

If so, try limiting the number of parallel downloads like so:

npm set maxsockets 3

Urban rumors

There are some rumors that are frequently reported, but don’t have any effect. For instance, http-proxy isn’t a parameter respected by npm, and it has never been.

As far as I know, npm only uses its own configuration files. The environment variables of the operating system are not read. However, they may be read be third-party tools calling npm indirectly. In particular, the Windows environment variables http-proxy and https-proxy are not read by npm.

Install your corporate npm proxy

If you’re experiencing problems with accessing the npm repository, I suggest setting up a corporate npm proxy. Among other things, this improves download times a lot. More often than not, the virus scanner of the central internet proxy is a major bottleneck. Using an intermediate npm proxy solves this problem because it serves as a cache.


Leave a Reply

Your email address will not be published.