Global Agent
global-agent is a Node.js library designed to globally configure HTTP/HTTPS proxy settings for outgoing network requests. It intercepts `http.Agent` and `https.Agent` instances to route traffic through a specified proxy server, configurable primarily via environment variables such as `GLOBAL_AGENT_HTTP_PROXY`, `GLOBAL_AGENT_HTTPS_PROXY`, and `GLOBAL_AGENT_NO_PROXY`. The current stable version is 4.1.3, released in March 2026, indicating an active development and maintenance status. The project maintains a steady release cadence, with multiple minor and patch updates in early 2026. Key differentiators include its simple bootstrap mechanism for seamless global integration across applications, support for dynamic runtime re-configuration of proxy settings, and its intentional design to override other explicitly configured HTTP agents. It distinguishes itself by primarily using its own set of environment variables rather than strictly adhering to the standard `HTTP_PROXY` convention for its main bootstrap module.
Common errors
-
Error: self signed certificate in certificate chain
cause The proxy server or target server is using a self-signed or untrusted SSL/TLS certificate, and `global-agent`'s `rejectUnauthorized` is `true` (default since v4.0.0).fixSet `GLOBAL_AGENT_SKIP_HTTPS_VALIDATION=true` in your environment variables to bypass certificate validation. Use with caution as it disables security checks. For production, ensure your proxy uses trusted certificates or configure your system to trust the self-signed certificate. -
Proxy is not being used, even with HTTP_PROXY set.
cause You are likely using the `HTTP_PROXY` environment variable, but `global-agent` expects `GLOBAL_AGENT_HTTP_PROXY` for its bootstrap module.fixExport `GLOBAL_AGENT_HTTP_PROXY` (e.g., `export GLOBAL_AGENT_HTTP_PROXY=http://localhost:8080`) instead of `HTTP_PROXY`. -
ERR_UNHANDLED_ERROR: Global agent is already initialized.
cause The `global-agent/bootstrap` module or the `bootstrap()` function has been called more than once in the application lifecycle.fixEnsure `import 'global-agent/bootstrap';` or `bootstrap();` is only executed once. If using conditional logic, wrap it carefully or use `createGlobalProxyAgent` for non-global instances. -
TypeError: (0, global_agent_1.bootstrap) is not a function
cause This error typically occurs when attempting to use a CommonJS `require()` syntax (e.g., `const { bootstrap } = require('global-agent');`) for named exports in a context where `global-agent` primarily provides ESM named exports, or when a default import was expected but named imports are now required.fixSwitch to ES module import syntax: `import { bootstrap } from 'global-agent';`. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json` or `.mjs` file extension).
Warnings
- breaking Starting from v4.0.0, `rejectUnauthorized` for TLS connections defaults to `true`. This aligns with Node.js's default behavior but means connections to servers with self-signed or otherwise invalid TLS certificates will now be rejected by default.
- gotcha The `global-agent/bootstrap` module and `bootstrap()` function primarily rely on `GLOBAL_AGENT_HTTP_PROXY` and `GLOBAL_AGENT_HTTPS_PROXY` environment variables, not the standard `HTTP_PROXY` or `HTTPS_PROXY`.
- gotcha `global-agent` is designed to aggressively override any explicitly configured `http.Agent` or `https.Agent` instances that are passed to Node.js's native `http`/`https` modules or popular libraries built on top of them (like `axios`).
- breaking Version 4.1.0 changed direct function exports to strictly named exports. While the side-effect `import 'global-agent/bootstrap'` remains, direct imports of functions like `bootstrap` and `createGlobalProxyAgent` must now use named import syntax.
Install
-
npm install global-agent -
yarn add global-agent -
pnpm add global-agent
Imports
- Side-effect bootstrap
const globalAgent = require('global-agent/bootstrap');import 'global-agent/bootstrap';
- bootstrap
const { bootstrap } = require('global-agent');import { bootstrap } from 'global-agent'; - createGlobalProxyAgent
const { createGlobalProxyAgent } = require('global-agent');import { createGlobalProxyAgent } from 'global-agent'; - GlobalAgent
import { GLOBAL_AGENT } from 'global-agent';declare var global: { GLOBAL_AGENT: GlobalProxyAgent };
Quickstart
import 'global-agent';
import http from 'node:http';
// Simulate setting the environment variable
process.env.GLOBAL_AGENT_HTTP_PROXY = process.env.GLOBAL_AGENT_HTTP_PROXY ?? 'http://127.0.0.1:8080';
console.log(`Global agent will use proxy: ${process.env.GLOBAL_AGENT_HTTP_PROXY}`);
// In a real scenario, you'd run this script with the environment variable set:
// export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080 && node your-script.js
// To explicitly bootstrap if not using the side-effect import:
// import { bootstrap } from 'global-agent';
// bootstrap();
const options = {
hostname: 'example.com',
port: 80,
path: '/',
method: 'GET'
};
const req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Response body length:', data.length);
});
});
req.on('error', (e) => {
console.error(`Problem with request: ${e.message}`);
});
req.end();