Starting from Node.js 24.0.0, fetch() supports NODE_USE_ENV_PROXY.

Starting from Node.js 24.0.0, fetch() supports NODE_USE_ENV_PROXY.

Example:

NODE_USE_ENV_PROXY=1 HTTP_PROXY=http://proxy.example.com:8080 HTTPS_PROXY=http://proxy.example.com:8080 NO_PROXY=localhost,127.0.0.1 node client.js

Documentation: https://nodejs.org/docs/latest/api/cli.html#cli_use_env_proxy

Starting from Node.js 24.5.0:

node:http and node:https now support proxies. When NODE_USE_ENV_PROXY is set to 1, the default global agent would parse the http_proxy/HTTP_PROXYhttps_proxy/HTTPS_PROXYno_proxy/NO_PROXY settings from the environment variables, and proxy the requests sent through the built-in http/https client accordingly.

Original answer below.


(March 2025) Node.js HTTP_PROXY support is almost here. When merged, you will be able to use an HTTP proxy using env vars only.

NODE_USE_ENV_PROXY=true HTTP_PROXY=proxy.org node app.js

Follow the PR if you want to be updated.

 

How NODE_USE_ENV_PROXY Works

When you set the environment variable NODE_USE_ENV_PROXY=1, you instruct Node.js's fetch() implementation to respect the standard proxy environment variables that are commonly used across the software industry:

  • HTTP_PROXY / http_proxy: The proxy server to use for HTTP requests.

  • HTTPS_PROXY / https_proxy: The proxy server to use for HTTPS requests. (Often set to the same as HTTP_PROXY).

  • NO_PROXY / no_proxy: A comma-separated list of hostnames, domains, or IP addresses that should bypass the proxy. Wildcards (*) are often supported (e.g., *.example.com).

Practical Example

Here is a more concrete example of how to use it, both from the command line and within a script.

1. From the Command Line (as you mentioned)

This is the most common use case, especially in containerized or managed environments where proxy settings are injected.

bash
 
# Set the environment variables and run your script
NODE_USE_ENV_PROXY=1 \
HTTP_PROXY="http://my-corp-proxy.internal:3128" \
HTTPS_PROXY="http://my-corp-proxy.internal:3128" \
NO_PROXY="localhost,127.0.0.1,.internal.example.com" \
node my-fetch-script.js

In my-fetch-script.js:

javascript
 
// No special code needed! fetch() automatically picks up the proxy settings.
const response = await fetch('https://api.github.com/users/github');
const data = await response.json();
console.log(data.name); // "GitHub"
// This request will go through the proxy defined in HTTPS_PROXY

const localResponse = await fetch('http://localhost:3000/api/data');
// This request will bypass the proxy due to NO_PROXY

2. Setting Environment Variables Programmatically (Inside your script)

While less common than setting them in the shell, you can also configure this within your application using process.env. This is useful if you need to dynamically set a proxy based on configuration.

Important: You must set these variables before the first call to fetch(). Once the HTTP agents are initialized, changing these variables won't have an effect.

javascript
 
// my-fetch-script.js
process.env.NODE_USE_ENV_PROXY = '1';
process.env.HTTP_PROXY = 'http://proxy.example.com:8080';
process.env.HTTPS_PROXY = 'http://proxy.example.com:8080';
process.env.NO_PROXY = 'localhost,127.0.0.1,.test';

// Now import or call code that uses fetch()
(async () => {
  try {
    // This will use the proxy
    const response = await fetch('https://httpbin.org/json');
    console.log('Request successful via proxy');
  } catch (error) {
    console.error('Error:', error);
  }
})();

Key Benefits

  1. Consistency: Behaves like curlwget, and many other CLI tools.

  2. No Extra Dependencies: You don't need libraries like proxy-agent for basic proxy support with fetch().

  3. Container/Enterprise Friendly: Simplifies configuration in environments where proxy settings are standardly passed via environment variables.

Important Considerations

  • Case Sensitivity: While the standard is often uppercase (HTTP_PROXY), many implementations (including Node's) also check for lowercase versions (http_proxy). It's safest to set both or use the case standard for your OS (uppercase on Windows, often either on Linux/macOS).

  • Precedence: The *_PROXY env vars are used only if NODE_USE_ENV_PROXY is set to 1. If it's not set, fetch() will try to make a direct connection.

  • Authentication: If your proxy requires authentication, include it in the URL:

    bash
     
    HTTPS_PROXY="http://username:password@proxy.example.com:8080"
  • It's for fetch() only: This mechanism specifically controls the behavior of the native fetch() API. It does not affect other HTTP clients like axios or got, or the legacy http/https modules. Those libraries have their own configuration methods for proxies.

This feature is a significant step forward for writing clean, portable Node.js applications that need to operate behind corporate firewalls or in complex network setups.

 
 
 

 

posted @ 2025-09-02 13:32  xosg  阅读(16)  评论(0)    收藏  举报