NPM and the Corporate Proxy

After installing node.js on my Windows 10 workstation at the office I tried to install some packages via npm and was greeted with the following error.

PS C:\Users\USER001> npm install handlebars -g
npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "handlebars" "-g"
npm ERR! node v6.2.2
npm ERR! npm  v3.9.5
npm ERR! code ECONNREFUSED
npm ERR! errno ECONNREFUSED
npm ERR! syscall connect

npm ERR! Error: connect ECONNREFUSED 23.235.44.162:443
npm ERR!     at Object.exports._errnoException (util.js:1007:11)
npm ERR!     at exports._exceptionWithHostPort (util.js:1030:20)
npm ERR!     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
npm ERR!  { Error: connect ECONNREFUSED 23.235.44.162:443
npm ERR!     at Object.exports._errnoException (util.js:1007:11)
npm ERR!     at exports._exceptionWithHostPort (util.js:1030:20)
npm ERR!     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
npm ERR!   code: 'ECONNREFUSED',
npm ERR!   errno: 'ECONNREFUSED',
npm ERR!   syscall: 'connect',
npm ERR!   address: '23.235.44.162',
npm ERR!   port: 443 }
npm ERR!
npm ERR! If you are behind a proxy, please make sure that the
npm ERR! 'proxy' config is set properly.  See: 'npm help config'

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\USER001\npm-debug.log
PS C:\Users\USER001>

To set the proxy use the following commands. Be sure to change the IP address and TCP port number to match your proxy server’s IP and port.

PS C:\Users\USER001> npm config set proxy http://10.10.10.10:8080
PS C:\Users\USER001> npm config set https-proxy http://10.10.10.10:8080

But if your corporate proxy is doing SSL interception and inspection you may still have another problem. You may get this certificate error if your proxy is using a certificate issued by a non-publicly trusted CA.

PS C:\Users\USER001> npm install handlebars -g
npm ERR! Windows_NT 10.0.10586
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "handlebars" "-g"
npm ERR! node v6.2.2
npm ERR! npm  v3.9.5
npm ERR! code UNABLE_TO_GET_ISSUER_CERT_LOCALLY

npm ERR! unable to get local issuer certificate
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\USER001\npm-debug.log
PS C:\Users\USER001>

There are two ways to handle this. The easier but less secure way to handle this is to configure npm to not use strict-ssl.

PS C:\Users\USER001> npm config set strict-ssl false

But a better and more secure way to resolve this error would be to configure npm to point to a pem file that contains the all the certificates in the certificate chain that your proxy server is using. On Windows make sure to double up the backslashes.

PS C:\Users\USER001> npm config set cafile="C:\\Certificates\\CAChain.pem"

When creating the pem file make sure that the certificate that the proxy server uses is listed first, followed by one or more intermediate certificates, and end with the root CA’s certificate.