|
| 1 | +--- |
| 2 | +title: Access environment variables on process.env in you Worker |
| 3 | +description: With Node.js compatability on, process.env is automatically populated with environment variables and secrets |
| 4 | +products: |
| 5 | + - workers |
| 6 | +date: 2025-03-07T11:00:00Z |
| 7 | +--- |
| 8 | + |
| 9 | +You can now access [environment variables](/workers/configuration/environment-variables/) and |
| 10 | +[secrets](/workers/configuration/secrets/) on [`process.env`](/workers/runtime-apis/nodejs/process/#processenv) |
| 11 | +when using the [`nodejs_compat` compatability flag](/workers/configuration/compatibility-flags/#nodejs-compatibility-flag). |
| 12 | + |
| 13 | +```js |
| 14 | +const apiClient = ApiClient.new({ apiKey: process.env.API_KEY }); |
| 15 | +const LOG_LEVEL = process.env.LOG_LEVEL || "info"; |
| 16 | +``` |
| 17 | + |
| 18 | +In Node.js, environment variable are exposed via the global `process.env` object. Some libraries |
| 19 | +assume that this object will be populated, and many developers may be used to accessing variables |
| 20 | +in this way. |
| 21 | + |
| 22 | +Previously, the `process.env` object was added when Node.js compatability was turned on, but it was empty. |
| 23 | +This could cause unexpected errors or friction when developing Workers with code previously written for |
| 24 | +Node.js. |
| 25 | + |
| 26 | +Now, [environment variables](/workers/configuration/environment-variables/), |
| 27 | +[secrets](/workers/configuration/secrets/), and [version metadata](/workers/runtime-apis/bindings/version-metadata/) |
| 28 | +can all be accessed on `process.env`. |
| 29 | + |
| 30 | +After April 1, 2025, populating `process.env` will become the default behavior when `nodejs_compat` is enabled, and |
| 31 | +your Worker's compatability date is after "2025-04-01". Until then, users can opt-in to this behavior by adding the |
| 32 | +[`nodejs_compat_populate_process_env`](/workers/configuration/compatibility-flags/#nodejs-compat-populate-process-env) |
| 33 | +compatability flag. |
0 commit comments