If you are behind a proxy and you try to run a npm command you will get an error similar to the one below:
npm ERR! network getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
To configure npm to use a proxy you need to set the proxy and https-proxy config variables. This can be done as follows (where <proxy>:<port> is the url and port of your proxy server):
npm config set proxy "http://<proxy>:<port>" npm config set https-proxy "http://<proxy>:<port>"
Now if you are behind a proxy that requires authentication you will see a slightly different error as shown below:
npm ERR! code ECONNRESET npm ERR! network tunneling socket could not be established, statusCode=407 npm ERR! network This is most likely not a problem with npm itself npm ERR! network and is related to network connectivity. npm ERR! network In most cases you are behind a proxy or have bad network settings. npm ERR! network npm ERR! network If you are behind a proxy, please make sure that the npm ERR! network 'proxy' config is set properly. See: 'npm help config'
Note that the 407 HTTP error code indicates that proxy authentication is required.
One way to resolve this issue is to use another proxy that is capable of accepting the npm requests and adding authentication headers when forwarding on the requests to the external proxy server (other ways of doing this involve including your username and password in the proxy config setting but I don’t recommend this approach in a corporate environment for security reasons).
There are several options for setting up an intermediate proxy but the one I’ve found easiest to setup is Fiddler. This approach is shown below with Fiddler adding the authentication headers when forwarding on npm requests to the proxy server that requires authentication.
The steps to configure this scenario are as follow:
- Download and install Fiddler
- In Fiddler select Rules > Automatically Authenticate (this is the setting that adds the NTLM authentication headers to each request)
- Run the following npm commands to configure npm to use Fiddler as an intermediate proxy (assuming Fiddler is using the default port):
npm config set proxy http://localhost:8888 npm config set https-proxy http://localhost:8888
Note that Fiddler will capture all network traffic by default. If you don’t want this behaviour you can deselect the Tools > Fiddler Options > Connections > Act as system proxy on startup option to only route npm traffic through Fiddler.
This also assumes that you have configured your proxy correctly in IE and can browse to the internet before applying the above settings.
Tip: To configure git to use the same configuration then run the following commands:
git config --global http.proxy http://localhost:8888 git config --global https.proxy http://localhost:8888
Credit goes to my colleague David Wright for pointing me in the direction of this approach.