To expand on @Steve Roberts answer.
My username is of the form "domain\username" - including the slash in
the proxy configuration resulted in a forward slash appearing. So
entering this:
npm config set proxy "http://domain\username:password@servername:port/"
I also had to URL encode my domain\user
string, however, I have a space inside my username so I put a +
to encode the space URL encoding, but it would get double encoded as %2B
(which is the URL encoding for the plus sign, however the URL encoding for a space is %20
), so I had to instead do the following:
npm command
// option one
// it works for some packages
npm config set http_proxy "http://DOMAIN%5Cuser+name:[email protected]:port"
npm config set proxy "http://DOMAIN%5Cuser+name:[email protected]:port"
// option two
// it works best for me
// please notice that I actually used a space
// instead of URL encode it with '+', '%20 ' OR %2B (plus url encoded)
npm config set http_proxy "http://DOMAIN%5Cuser name:[email protected]:port"
npm config set proxy "http://DOMAIN%5Cuser name:[email protected]:port"
// option two (B) as of 2019-06-01
// no DOMAIN
// instead of URL encode it with '+', '%20 ' OR %2B (plus url encoded)
npm config set http_proxy "http://user name:[email protected]:port"
npm config set proxy "http://user name:[email protected]:port"
troubleshooting npm config
I used the npm config list
to get the parsed values that I had set above, and that is how I found out about the double encoding. Weird.
Essentially you must figure out the following requirements:
- Is a
DOMAIN
string required for authentication
- Do you need to encode special characters?
- Spaces and at (@) signs are specially challenging
Regards.
WINDOWS ENVIRONMENT VARIABLES (CMD Prompt)
Update
Turns out that even with the above configurations, I still had some issues with some packages/scripts that use Request - Simplified HTTP client internally to download stuff. So, as the above readme explained, we can specify environment variables to set the proxy on the command line, and Request will honor those values.
Then, after (and I am reluctant to admit this) several tries (more like days), of trying to set the environment variables I finally succeeded with the following guidelines:
rem notice that the value after the = has no quotations
rem - I believe that if quotations are placed after it, they become
rem part of the value, you do not want that
rem notice that there is no space before or after the = sign
rem - if you leave a space before it, you will be declaring a variable
rem name that includes such space, you do not want to do that
rem - if you leave a space after it, you will be including the space
rem as part of the value, you do not want that either
rem looks like there is no need to URL encode stuff in there
SET HTTP_PROXY=http://DOMAIN\user name:[email protected]:port
SET HTTPS_PROXY=http://DOMAIN\user name:[email protected]:port
cntlm
I used the above technique for a few weeks, untill I realized the overhead of updating my password across all the tools that needed the proxy setup.
Besides npm, I also use:
- bower
- vagrant
- virtual box (running linux)
- apt-get [linux]
- git
- vscode
- brackets
- atom
- tsd
cntlm Setup Steps
So, I installed cntlm. Setting cntlm
is pretty stright forward, you look for the ini file @ C:\Program Files\Cntlm\cntlm.ini
- Open
C:\Program Files\Cntlm\cntlm.ini
(you may need admin rights)
- look for
Username
and Domain
lines (line 8-9 I think)
- add your username
- add your domain
On cmd prompt run:
cd C:\Program Files\Cntlm\
cntlm -M
cntlm -H
- you will be asked for the password:
cygwin warning:
MS-DOS style path detected: C:\Program Files\Cntlm\cntlm.ini
Preferred POSIX equivalent is: /Cntlm/cntlm.ini
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Password:
The output you get from cntlm -H
will look something like:
PassLM 561DF6AF15D5A5ADG
PassNT A1D651A5F15DFA5AD
PassNTLMv2 A1D65F1A65D1ASD51 # Only for user 'user name', domain 'DOMAIN'
- It is recomended that you use PassNTLMv2 so add a
#
before line PassLM
and PassNT
or do not use them
- Paste the output from
cntlm -H
on the ini file replacing the lines for PassLM
, PassNT
and PassNTMLv2
, or comment the original lines and add yours.
- Add your
Proxy
servers. If you do not know what the proxy server is... Do what I did, I looked for my proxy auto-config file by looking for the AutoConfigURL
Registry key in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings
. Navigate to that url and look through the code which happens to be JavaScript.
- Optionaly you can change the port where cntlm listens to by changing the
Listen ####
line, where ####
is the port number.
Setup NPM with cntlm
So, you point npm to your cntml proxy, you can use the ip, I used localhost
and the default port for cntlm 3128
so my proxy url looks like this
http://localhost:3128
With the proper command:
npm config set proxy http://localhost:3128
Is a lot simpler. You setup all your tools with that same url, and you only update the password on one place. Life is so much simpler not.
Must Setup The npm CA certificate
From the npm documentation ca
If your corporate proxy is intercepting https connections with its own Self Signed Certificate, this is a must to avoid npm config set strict-ssl false
(big no-no).
Basic steps
- Get the certificate from your browser (Chromes works well). Export it as Base-64 encoded X.509 (.CER)
- Replace new lines with
\n
- Edit your
.npmrc
add a line ca[]="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"
Issues
I have noticed tha sometimes npm kind of hangs, so I stop (sometimes forcefully) cntlm and restart it.
proxy.pac
file in order to get proxy address from it. (In my case I simply had to switch port to 8080). – nomad