|
| 1 | +--- |
| 2 | +title: Enterprise Network Configuration |
| 3 | +layout: learn |
| 4 | +authors: Joyee Cheung |
| 5 | +--- |
| 6 | + |
| 7 | +# Enterprise Network Configuration |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +Enterprise environments often require applications to operate behind corporate proxies and use custom certificate authorities (CAs) for SSL/TLS validation. Node.js provides built-in support for these requirements through environment variables and command-line flags, eliminating the need for third-party proxy libraries in many cases. |
| 12 | + |
| 13 | +This guide covers how to configure Node.js applications to work in enterprise network environments: |
| 14 | + |
| 15 | +- Configuring proxies via the `NODE_USE_ENV_PROXY` environment variable or the `--use-env-proxy` flag |
| 16 | +- Adding certificate authorities from system store via the `NODE_USE_SYSTEM_CA` environment variable or the `--use-system-ca` flag. |
| 17 | + |
| 18 | +## Proxy Configuration |
| 19 | + |
| 20 | +In many enterprise environments, internet access to external services may need to be routed through HTTP/HTTPS proxies for security and monitoring. This requires applications to be aware of and use these proxies when making network requests. |
| 21 | + |
| 22 | +Proxy settings are often provided via environment variables such as `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY`. Node.js supports these when `NODE_USE_ENV_PROXY` or `--use-env-proxy` is enabled. This works with `node:http` and `node:https` (v22.21.0 or v24.5.0+) methods as well as `fetch()` (v22.21.0 or v24.0.0+). |
| 23 | + |
| 24 | +Example (POSIX shells): |
| 25 | + |
| 26 | +```bash |
| 27 | +# The proxy settings might be configured in the system by your IT department |
| 28 | +# and shared across different tools. |
| 29 | +export HTTP_PROXY=http://proxy.company.com:8080 |
| 30 | +export HTTPS_PROXY=http://proxy.company.com:8080 |
| 31 | +export NO_PROXY=localhost,127.0.0.1,.company.com |
| 32 | + |
| 33 | +# To enable it for Node.js applications. |
| 34 | +export NODE_USE_ENV_PROXY=1 |
| 35 | +node app.js |
| 36 | +``` |
| 37 | + |
| 38 | +Alternatively, enable it via the command-line flag `--use-env-proxy` on Node.js v22.21.0 or v24.5.0 and above: |
| 39 | + |
| 40 | +```bash |
| 41 | +# The proxy settings might be configured in the system by your IT department |
| 42 | +# and shared across different tools. |
| 43 | +export HTTP_PROXY=http://proxy.company.com:8080 |
| 44 | +export HTTPS_PROXY=http://proxy.company.com:8080 |
| 45 | +export NO_PROXY=localhost,127.0.0.1,.company.com |
| 46 | + |
| 47 | +# To enable it for Node.js applications. |
| 48 | +node --use-env-proxy app.js |
| 49 | +``` |
| 50 | + |
| 51 | +Or, if `--env-file` is used to load environment variables from a file: |
| 52 | + |
| 53 | +```txt |
| 54 | +# In .env file |
| 55 | +HTTP_PROXY=http://proxy.company.com:8080 |
| 56 | +HTTPS_PROXY=http://proxy.company.com:8080 |
| 57 | +NO_PROXY=localhost,127.0.0.1,.company.com |
| 58 | +NODE_USE_ENV_PROXY=1 |
| 59 | +``` |
| 60 | + |
| 61 | +```bash |
| 62 | +node --env-file ./.env app.js |
| 63 | +``` |
| 64 | + |
| 65 | +Once enabled, `http`, `https`, and `fetch()` requests use the configured proxies by default, unless an agent is overridden or the target matches `NO_PROXY`. |
| 66 | + |
| 67 | +### Configure the Proxy Programmatically |
| 68 | + |
| 69 | +To configure the proxy programmatically, override the agents. This is currently supported by `https.request()` and other methods built upon it such as `https.get()`. |
| 70 | + |
| 71 | +To override the agent on a per-request basis, use the `agent` option for `http.request()`/`https.request()` and similar methods: |
| 72 | + |
| 73 | +```cjs |
| 74 | +const https = require('node:https'); |
| 75 | + |
| 76 | +// Creating a custom agent with custom proxy support. |
| 77 | +const agent = new https.Agent({ |
| 78 | + proxyEnv: { HTTPS_PROXY: 'http://proxy.company.com:8080' }, |
| 79 | +}); |
| 80 | + |
| 81 | +https.request( |
| 82 | + { |
| 83 | + hostname: 'www.external.com', |
| 84 | + port: 443, |
| 85 | + path: '/', |
| 86 | + agent, |
| 87 | + }, |
| 88 | + res => { |
| 89 | + // This request will be proxied through proxy.company.com:8080 using the HTTP protocol. |
| 90 | + } |
| 91 | +); |
| 92 | +``` |
| 93 | + |
| 94 | +```mjs |
| 95 | +import https from 'node:https'; |
| 96 | + |
| 97 | +// Creating a custom agent with custom proxy support. |
| 98 | +const agent = new https.Agent({ |
| 99 | + proxyEnv: { HTTPS_PROXY: 'http://proxy.company.com:8080' }, |
| 100 | +}); |
| 101 | + |
| 102 | +https.request( |
| 103 | + { |
| 104 | + hostname: 'www.external.com', |
| 105 | + port: 443, |
| 106 | + path: '/', |
| 107 | + agent, |
| 108 | + }, |
| 109 | + res => { |
| 110 | + // This request will be proxied through proxy.company.com:8080 using the HTTP protocol. |
| 111 | + } |
| 112 | +); |
| 113 | +``` |
| 114 | + |
| 115 | +To override the agent globally, reset `http.globalAgent` and `https.globalAgent`: |
| 116 | + |
| 117 | +<!-- TODO(joyeecheung): update this when Node.js has a method that supports global configuration for all requesters --> |
| 118 | + |
| 119 | +**Note**: Global agents do not affect `fetch()`. |
| 120 | + |
| 121 | +```cjs |
| 122 | +const http = require('node:http'); |
| 123 | +const https = require('node:https'); |
| 124 | + |
| 125 | +http.globalAgent = new http.Agent({ |
| 126 | + proxyEnv: { HTTP_PROXY: 'http://proxy.company.com:8080' }, |
| 127 | +}); |
| 128 | +https.globalAgent = new https.Agent({ |
| 129 | + proxyEnv: { HTTPS_PROXY: 'http://proxy.company.com:8080' }, |
| 130 | +}); |
| 131 | + |
| 132 | +// Subsequent requests will all use the configured proxies, unless they override the agent option. |
| 133 | +http.request('http://external.com', res => { |
| 134 | + /* ... */ |
| 135 | +}); |
| 136 | +https.request('https://external.com', res => { |
| 137 | + /* ... */ |
| 138 | +}); |
| 139 | +``` |
| 140 | + |
| 141 | +```mjs |
| 142 | +import http from 'node:http'; |
| 143 | +import https from 'node:https'; |
| 144 | + |
| 145 | +http.globalAgent = new http.Agent({ |
| 146 | + proxyEnv: { HTTP_PROXY: 'http://proxy.company.com:8080' }, |
| 147 | +}); |
| 148 | +https.globalAgent = new https.Agent({ |
| 149 | + proxyEnv: { HTTPS_PROXY: 'http://proxy.company.com:8080' }, |
| 150 | +}); |
| 151 | + |
| 152 | +// Subsequent requests will all use the configured proxies, unless they override the agent option. |
| 153 | +http.request('http://external.com', res => { |
| 154 | + /* ... */ |
| 155 | +}); |
| 156 | +https.request('https://external.com', res => { |
| 157 | + /* ... */ |
| 158 | +}); |
| 159 | +``` |
| 160 | + |
| 161 | +### Using Proxies with Authentication |
| 162 | + |
| 163 | +If the proxy requires authentication, include credentials in the proxy URL: |
| 164 | + |
| 165 | +```bash |
| 166 | +export HTTPS_PROXY=http://username: [email protected]:8080 |
| 167 | +``` |
| 168 | + |
| 169 | +**Security Note**: Avoid committing credentials in env files. Prefer a secret manager and programmatic configuration. |
| 170 | + |
| 171 | +### Proxy Bypass Configuration |
| 172 | + |
| 173 | +The `NO_PROXY` variable supports: |
| 174 | + |
| 175 | +- `*` - Bypass proxy for all hosts |
| 176 | +- `company.com` - Exact host name match |
| 177 | +- `.company.com` - Domain suffix match (matches `sub.company.com`) |
| 178 | +- `*.company.com` - Wildcard domain match |
| 179 | +- `192.168.1.100` - Exact IP address match |
| 180 | +- `192.168.1.1-192.168.1.100` - IP address range |
| 181 | +- `company.com:8080` - Hostname with specific port |
| 182 | + |
| 183 | +If a target matches `NO_PROXY`, the request bypasses the proxy. |
| 184 | + |
| 185 | +## Certificate Authority Configuration |
| 186 | + |
| 187 | +By default, Node.js uses Mozilla’s bundled root CAs and does not consult the OS store. In many enterprise environments, internal CAs are installed in the OS store and are expected to be trusted when connecting to internal services; connections to certificates signed by those CAs can fail validation with errors such as: |
| 188 | + |
| 189 | +``` |
| 190 | +Error: self signed certificate in certificate chain |
| 191 | +``` |
| 192 | + |
| 193 | +From Node.js v22.15.0, v23.9.0, v24.0.0 and above, Node.js can be configured to trust these custom CAs using the system's certificate store. |
| 194 | + |
| 195 | +### Adding CA Certificates from the System Store |
| 196 | + |
| 197 | +- From environment variable: `NODE_USE_SYSTEM_CA=1 node app.js` |
| 198 | +- From command-line flag: `node --use-system-ca app.js` |
| 199 | + |
| 200 | +When enabled, Node.js loads system CAs and uses them in addition to its bundled CAs for TLS validation. |
| 201 | + |
| 202 | +Node.js reads certificates from different locations depending on the platform: |
| 203 | + |
| 204 | +- Windows: Windows Certificate Store (via Windows Crypto API) |
| 205 | +- macOS: macOS Keychain |
| 206 | +- Linux: OpenSSL defaults, typically via `SSL_CERT_FILE`/`SSL_CERT_DIR`, or paths like `/etc/ssl/cert.pem` and `/etc/ssl/certs/` depending on the OpenSSL build |
| 207 | + |
| 208 | +Node.js follows a policy similar to that of Chromium. See [the Node.js documentation](https://nodejs.org/api/cli.html#--use-system-ca) for more details. |
| 209 | + |
| 210 | +### Adding additional CA Certificates |
| 211 | + |
| 212 | +To add specific CA certificates without relying on the system store: |
| 213 | + |
| 214 | +```bash |
| 215 | +export NODE_EXTRA_CA_CERTS=/path/to/company-ca-bundle.pem |
| 216 | +node app.js |
| 217 | +``` |
| 218 | + |
| 219 | +The file should contain one or more PEM-encoded certificates. |
| 220 | + |
| 221 | +#### Combining Options |
| 222 | + |
| 223 | +You can combine `NODE_USE_SYSTEM_CA` with `NODE_EXTRA_CA_CERTS`: |
| 224 | + |
| 225 | +```bash |
| 226 | +export NODE_USE_SYSTEM_CA=1 |
| 227 | +export NODE_EXTRA_CA_CERTS=/path/to/additional-cas.pem |
| 228 | +node app.js |
| 229 | +``` |
| 230 | + |
| 231 | +With both enabled, Node.js trusts bundled CAs, system CAs, and the additional certificates specified by `NODE_EXTRA_CA_CERTS`. |
| 232 | + |
| 233 | +### Configure CA Certificates Programmatically |
| 234 | + |
| 235 | +#### Configure Global CA Certificates |
| 236 | + |
| 237 | +Use [`tls.getCACertificates()`](https://nodejs.org/api/tls.html#tlsgetcacertificatestype) and [`tls.setDefaultCACertificates()`](https://nodejs.org/api/tls.html#tlssetdefaultcacertificatescerts) to configure global CA certificates. For example, to add system certificates into the default store: |
| 238 | + |
| 239 | +```cjs |
| 240 | +const https = require('node:https'); |
| 241 | +const tls = require('node:tls'); |
| 242 | +const currentCerts = tls.getCACertificates('default'); |
| 243 | +const systemCerts = tls.getCACertificates('system'); |
| 244 | +tls.setDefaultCACertificates([...currentCerts, ...systemCerts]); |
| 245 | + |
| 246 | +// Subsequent requests use system certificates during verification. |
| 247 | +https.get('https://internal.company.com', res => { |
| 248 | + /* ... */ |
| 249 | +}); |
| 250 | +fetch('https://internal.company.com').then(res => { |
| 251 | + /* ... */ |
| 252 | +}); |
| 253 | +``` |
| 254 | + |
| 255 | +```mjs |
| 256 | +import https from 'node:https'; |
| 257 | +import tls from 'node:tls'; |
| 258 | +const currentCerts = tls.getCACertificates('default'); |
| 259 | +const systemCerts = tls.getCACertificates('system'); |
| 260 | +tls.setDefaultCACertificates([...currentCerts, ...systemCerts]); |
| 261 | + |
| 262 | +// Subsequent requests use system certificates during verification. |
| 263 | +https.get('https://internal.company.com', res => { |
| 264 | + /* ... */ |
| 265 | +}); |
| 266 | +fetch('https://internal.company.com').then(res => { |
| 267 | + /* ... */ |
| 268 | +}); |
| 269 | +``` |
| 270 | + |
| 271 | +#### Configure CA Certificates for Individual Requests |
| 272 | + |
| 273 | +To override CA certificates per request, use the `ca` option. This is currently only supported by `tls.connect()`/`https.request()` and methods built upon them such as `https.get()`. |
| 274 | + |
| 275 | +```cjs |
| 276 | +const https = require('node:https'); |
| 277 | +const specialCerts = ['-----BEGIN CERTIFICATE-----\n...']; |
| 278 | +https.get( |
| 279 | + { |
| 280 | + hostname: 'internal.company.com', |
| 281 | + port: 443, |
| 282 | + path: '/', |
| 283 | + method: 'GET', |
| 284 | + // The `ca` option replaces defaults; concatenate bundled certs if needed. |
| 285 | + ca: specialCerts, |
| 286 | + }, |
| 287 | + res => { |
| 288 | + /* ... */ |
| 289 | + } |
| 290 | +); |
| 291 | +``` |
| 292 | + |
| 293 | +```mjs |
| 294 | +import https from 'node:https'; |
| 295 | +const specialCerts = ['-----BEGIN CERTIFICATE-----\n...']; |
| 296 | +https.get( |
| 297 | + { |
| 298 | + hostname: 'internal.company.com', |
| 299 | + port: 443, |
| 300 | + path: '/', |
| 301 | + method: 'GET', |
| 302 | + // The `ca` option replaces defaults; concatenate bundled certs if needed. |
| 303 | + ca: specialCerts, |
| 304 | + }, |
| 305 | + res => { |
| 306 | + /* ... */ |
| 307 | + } |
| 308 | +); |
| 309 | +``` |
0 commit comments