npm behind a corporate proxy - how to set your proxy and https-proxy environment variables, including your domain and escape characters

Sitting behind a corporate proxy server and trying to run any modern command line tools that rely on internet connection means you are destined to tinker with some environment variables. And so it was with me, having set up a fresh install of Ubuntu, I exposed a couple of variables in /etc/environment to pass my credentials through the proxy:

http_proxy="http://domain\\user.name:password@proxy.ip.address:port"
https_proxy="http://domain\\user.name:password@proxy.ip.address:port"

My proxy server relies on fully qualified domain and user name, hence the double backslash - one backslash to escape another. This worked well for git and some elixir tinkering I was doing, but it all went belly up when I tried to use npm. A nice error message from npm and some gut feeling left me tinkering with npm specific environment vars to get thinks working. I discovered that npm is not as tolerant of escaping special characters as git and mix. Instead, an ASCII code is needed to denote the backslash.

{% highlight bash %}
npm_config_https_proxy=http://domain%5Cuser.name:password@proxy.ip.address:port
npm_config_proxy=http://domain%5Cuser.name:password@proxy.ip.address:port
{% endhighlight %}

Exposing these two npm specific environment variables in my .bash_profile did the trick.

Somewhat confusingly, listing your npm config returns your npm proxy variables without the npm_config prefix and also turns the underscore to a dash, so you’ll end up with something like the following if you issue npm config list at your command line:

https-proxy = "http://domain%5Cuser.name:password@proxy.ip.address:port"
proxy = "http://domain%5Cuser.name:password@proxy.ip.address:port"

Don’t worry, it all works.

Once my proof of concept worked and npm was happy, I updated my /etc/environment to use the ASCII code instead of the escaped backslash and removed the npm specific environment variables from my .bash_profile. This left a neater solution with only one set of variables defined. Git is still happy, as is mix, so there you go.