Working with NPM behind Network Proxy
Introduction
Talking about working with Javascript, using Node Package Manager (NPM) is a common practice lately to manage the packages and dependencies of Javascript projects. NPM can make developers download and manage packages with its functions and scripts. However, NPM depends on internet to download the packages. So, what if the network we use actually equipped with proxy which mostly happens in company’s network?
Configure Proxy for NPM
In a network with Proxy, the download process of packages may unable to be done. Thus, will we unable to get the packages easily like we usually do when using network without proxy? Actually, it’s not extreme like that. NPM has its workaround to resolve this issue. This only takes around three lines:
The next question is, What if the proxy requires specific domain, proxy username, and password? This is the full format of the URL to replace the http://[YOUR PROXY HOST]:[PORT]
on the template above.
"http://[DOMAIN]%5C[USERNAME]:[PASSWORD]@[HOST]:[PORT]"
The field [DOMAIN]
, [USERNAME]
, and [PASSWORD]
above are optional (based on your proxy requirements). As for the https-proxy
variable, just keep using the http://
instead of https://
.
Even with the three commands above, you may get another error, i.e. SSL Error. To overcome this, there are two methods can be used.
- Disabling SSL (unsafe not recommended).
npm config set strict-ssl false
2. Configuring certificate while SSL authentication is true (recommended)
npm config set strict-ssl true
npm config -g set cafile [YOUR CERTIFICATE DIR]/[CERTIFICATE NAME].crt
Configuring NPM If Network is Behind Proxy .pac
You may simply download the .pac
file and pick any proxy with http://
to be the proxy URL for the above configurations.
Other workaround is using Alpaca.
Recheck The Proxy Configurations for NPM
Before running the npm install
or npm update
to run the dependency download, it’s better to check whether the proxy is already set or not by running these lines on CMD/Terminal and ensure if the proxies set correctly for each variables.
npm config get proxy
npm config get http-proxy
npm config get https-proxy
You can check whole config of your npm
by running this line:
npm config list
Configure Environment Variables for Proxy
Sometimes, there are packages that execute their script at post install or when installing that may cause failure on installing the packages. Just imagine if you have existing project with tons of dependencies, you waited for minutes to install it with npm install
but it ended as failure because of a package that execute its internal script that doing request to another websites. Unfortunately, some packages looks for system environment variables rather than using the one from NPM Config variables, thus you may need to set the system environment variables for http_proxy
, https_proxy
, and proxy
.
For Windows users, you may just copy paste this line below, replace the [URL]
with the one you use before for the NPM Proxy config, and then run it in Windows Powershell:
$env:HTTP_PROXY=[URL]
$env:HTTPS_PROXY=[URL]
And for the LINUX/UNIX users, you can run this instead:
export http_proxy='[URL]'
export https_proxy='[URL]'
Conclusion
If you have the proxy server working well and you have set your device to use the proxy, you should be all set already. This is my first article in medium that I would like to share based on my experience. There would be many other experiences in this topic as well. I am open for another workaround, so feel free to write a feedback for me! Cheers and Thanks for reading.