You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
compatibility flags, and the `disallow_importable_env` compatibility flag is
107
+
not set, environment variables will also be available via the global
108
+
`process.env`. Note that the `nodejs_compat_populate_process_env` flag is
109
+
enabled automatically when `nodejs_compat` is used with a compatibility date
110
+
on or after April 1st, 2025.
111
+
112
+
The `process.env` will be populated lazily the first time that `process` is accessed
113
+
in the worker.
114
+
115
+
Text variable values are exposed directly.
116
+
117
+
JSON variable values that evaluate to string values are exposed as the parsed value.
118
+
119
+
JSON variable values that do not evaluate to string values are exposed as the raw
120
+
JSON string.
121
+
122
+
For example, imagine a Worker with three environment variables, two text values, and
123
+
one JSON value:
124
+
125
+
```
126
+
[vars]
127
+
FOO = "abc"
128
+
BAR = "abc"
129
+
BAZ = { "a": 123 }
130
+
```
131
+
132
+
Environment variables can be added using either the `wrangler.{json|jsonc|toml}` file or via the Cloudflare
133
+
dashboard UI.
134
+
135
+
The values of `process.env.FOO` and `process.env.BAR` will each be the
136
+
JavaScript string `"abc"`.
137
+
138
+
The value of `process.env.BAZ` will be the JSON-encoded string `"{ \"a\": 123 }"`.
139
+
140
+
:::note
141
+
Note also that because secrets are a form of environment variable within the runtime,
142
+
secrets are also exposed via `process.env`.
143
+
102
144
## Related resources
103
145
104
146
- Migrating environment variables from [Service Worker format to ES modules syntax](/workers/reference/migrate-to-module-workers/#environment-variables).
Copy file name to clipboardExpand all lines: src/content/docs/workers/configuration/secrets.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ import { Render } from "~/components";
9
9
10
10
## Background
11
11
12
-
Secrets are a type of binding that allow you to attach encrypted text values to your Worker. You cannot see secrets after you set them and can only access secrets via [Wrangler](/workers/wrangler/commands/#secret) or programmatically via the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters). Secrets are used for storing sensitive information like API keys and auth tokens. Secrets are available on the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters) passed to your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/).
12
+
Secrets are a type of binding that allow you to attach encrypted text values to your Worker. Secrets are used for storing sensitive information like API keys and auth tokens. Secrets are programmatically available on the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters) passed to your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/), and may also be accessible via [`process.env`](/workers/configuration/environment-variables) in Workers that support Node.js compatibility.
13
13
14
14
## Access your secrets with Workers
15
15
@@ -111,4 +111,4 @@ To delete a secret from your Worker project via the dashboard:
111
111
## Related resources
112
112
113
113
-[Wrangler secret commands](/workers/wrangler/commands/#secret) - Review the Wrangler commands to create, delete and list secrets.
114
-
-[Cloudflare Secrets Store](/secrets-store/) - Encrypt and store sensitive information as secrets that are securely reusable across your account.
114
+
-[Cloudflare Secrets Store](/secrets-store/) - Encrypt and store sensitive information as secrets that are securely reusable across your account.
Copy file name to clipboardExpand all lines: src/content/docs/workers/runtime-apis/nodejs/process.mdx
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,16 @@ When [Node.js compatibility](/workers/runtime-apis/nodejs/) is turned on and the
33
33
34
34
### Relationship to per-request `env` argument in `fetch()` handlers
35
35
36
-
Workers do have a concept of [environment variables](/workers/configuration/environment-variables/) that are applied on a per-Worker and per-request basis. These are not accessible automatically via the `process.env` API. It is possible to manually copy these values into `process.env` if you need to. Be aware, however, that setting any value on `process.env` will coerce that value into a string.
36
+
Workers have a concept of [environment variables](/workers/configuration/environment-variables/) that are applied on a per-Worker and per-request basis.
37
+
38
+
By default, these are not accessible via the `process.env` API.
39
+
To automatically populate environment variables and secrets on `process.env`, enable
40
+
the [`nodejs_compat_populate_process_env`](/workers/configuration/compatibility-flags/#nodejs_compat_populate_process_env)
41
+
compatibility flag and disable the `disallow_importable_env` compatibility flag.
42
+
It is also possible to manually copy these values into `process.env` if
43
+
necessary -- but only within the context of a request.
44
+
45
+
Setting any value on `process.env` will coerce that value into a string.
37
46
38
47
```js
39
48
import*asprocessfrom'node:process';
@@ -51,7 +60,11 @@ export default {
51
60
};
52
61
```
53
62
54
-
It is strongly recommended that you *do not* replace the entire `process.env` object with the request `env` object. Doing so will cause you to lose any environment variables that were set previously and will cause unexpected behavior for other Workers running in the same isolate. Specifically, it would cause inconsistency with the `process.env` object when accessed via named imports.
63
+
It is strongly recommended that you *do not* replace the entire `process.env` object with
64
+
the cloudflare `env` object. Doing so will cause you to lose any environment variables that
65
+
were set previously and will cause unexpected behavior for other Workers running in the
66
+
same isolate. Specifically, it would cause inconsistency with the `process.env` object when
67
+
accessed via named imports.
55
68
56
69
```js
57
70
import*asprocessfrom'node:process';
@@ -63,7 +76,6 @@ process.env === env; // false! they are no longer the same object
63
76
64
77
// From this point forward, any changes to process.env will not be reflected in env,
0 commit comments