When using npm, you may encounter a situation where you need to access the npm registry through a proxy server, but you also want certain specific domains to bypass the proxy directly. This requirement is particularly common when working with private npm repositories in enterprise environments. Below is how to exclude specific domains from npm's proxy settings:
-
Set npm Proxy First, configure the npm proxy using the
npm configcommand. For example, if your proxy server address ishttp://proxy.example.com:8080, set the HTTP and HTTPS proxies as follows:bashnpm config set proxy http://proxy.example.com:8080 npm config set https-proxy http://proxy.example.com:8080 -
Use
no_proxyto Exclude Specific Domains To exclude certain domains from the proxy, utilize theno_proxyenvironment variable. This variable specifies domains that should bypass the proxy. For instance, if you wantexample.organdmyprivate.repoto access directly without the proxy, set:bashnpm config set no-proxy example.org,myprivate.repoAlternatively, configure it directly in your environment variables:
bashexport NO_PROXY=example.org,myprivate.repoOn Windows, add the
NO_PROXYvariable through the environment variable settings. -
Verify Configuration After setting up, confirm your configuration by checking npm's settings:
bashnpm config listThis command displays all npm configuration details, including your proxy settings and excluded domains.
-
Example Scenario Consider an enterprise environment with a private npm repository at
myprivate.repoand the need to access the public npm registry. The company's network policy mandates that all external access must go through a proxy. Here, configure the npm proxy and addmyprivate.repotono_proxyto ensure direct access to the private repository, maintaining both speed and security.
With this setup, you can access external resources via the proxy while directly connecting to specific internal or sensitive domains, satisfying security policies and optimizing network efficiency.