npm - Set proxy settings

By xngo on August 15, 2019

This article describes how to set npm to use your proxy settings.

Symptoms

If you are behind a corporate proxy server and you didn't set npm to use the proxy settings, here are the error messages that you will get if you are trying to run it. For example, you run it to upgrade itself to the latest version.

C:\Users\xuan.ngo>npm install npm@latest -g
npm ERR! code UNABLE_TO_VERIFY_LEAF_SIGNATURE
npm ERR! errno UNABLE_TO_VERIFY_LEAF_SIGNATURE
npm ERR! request to https://registry.npmjs.org/npm failed, reason: unable to verify the first certificate

Even though you run npm config set strict-ssl false to blindly accept untrusted or invalid SSL certificates, you will still get error messages.

C:\Users\xuan.ngo>npm install npm@latest -g
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/npm
npm ERR! 404
npm ERR! 404  'npm@latest' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

Get proxy settings

Before you set the proxy settings for npm, you first need to know what they are. You either ask your system administrator or get the information from Internet Explorer. It is much easier to get these information from Internet Explorer than from the system administrator. Here is how to get the information from Internet Explorer.

  1. Open the Internet options of the Internet Explorer.
  2. Navigate to the Connections tab.
  3. At the bottom right, click on the LAN settings button.
  4. The value in the Address input field is the proxy settings that you need. What is before the colon(:) is the server address. What is after the colon(:) is the port number.
    IE - Proxy settings

Set npm to use proxy

Run the command below to set npm to use the proxy.

npm config set https-proxy http://some.proxy.ip.com:8087

Test

Let's test it to see whether it can update itself to the latest version by running the following command.

C:\Users\xuan.ngo>npm install npm@latest -g
C:\Users\xuan.ngo\AppData\Roaming\npm\npm -> C:\Users\xuan.ngo\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
C:\Users\xuan.ngo\AppData\Roaming\npm\npx -> C:\Users\xuan.ngo\AppData\Roaming\npm\node_modules\npm\bin\npx-cli.js
+ npm@6.10.3
added 430 packages from 828 contributors in 81.607s

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.