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
[docs] Warn/clarify that env vars are NOT "SECRET" (#6062)
* [docs] Warn about storing secrets in env vars
Fixes#5676
Co-Authored-By: Ian Schmitz <[email protected]>
* [docs] Add NOT to REACT_APP_SECRET_CODE
Fixes#5676
* [docs] Remove line breaks
> Note: this feature is available with `[email protected]` and higher.
8
8
9
-
Your project can consume variables declared in your environment as if they were declared locally in your JS files. By
10
-
default you will have `NODE_ENV` defined for you, and any other environment variables starting with
11
-
`REACT_APP_`.
9
+
Your project can consume variables declared in your environment as if they were declared locally in your JS files. By default you will have `NODE_ENV` defined for you, and any other environment variables starting with `REACT_APP_`.
10
+
11
+
> WARNING: Do not store any secrets (such as private API keys) in your React app!
12
+
>
13
+
> Environment variables are embedded into the build, meaning anyone can view them by inspecting your app's files.
12
14
13
15
**The environment variables are embedded during the build time**. Since Create React App produces a static HTML/CSS/JS bundle, it can’t possibly read them at runtime. To read them at runtime, you would need to load HTML into memory on the server and replace placeholders in runtime, just like [described here](title-and-meta-tags.md#injecting-data-from-the-server-into-the-page). Alternatively you can rebuild the app on the server anytime you change them.
14
16
15
17
> Note: You must create custom environment variables beginning with `REACT_APP_`. Any other variables except `NODE_ENV` will be ignored to avoid accidentally [exposing a private key on the machine that could have the same name](https://github.com/facebook/create-react-app/issues/865#issuecomment-252199527). Changing any environment variables will require you to restart the development server if it is running.
16
18
17
-
These environment variables will be defined for you on `process.env`. For example, having an environment
18
-
variable named `REACT_APP_SECRET_CODE` will be exposed in your JS as `process.env.REACT_APP_SECRET_CODE`.
19
+
These environment variables will be defined for you on `process.env`. For example, having an environment variable named `REACT_APP_NOT_SECRET_CODE` will be exposed in your JS as `process.env.REACT_APP_NOT_SECRET_CODE`.
19
20
20
21
There is also a special built-in environment variable called `NODE_ENV`. You can read it from `process.env.NODE_ENV`. When you run `npm start`, it is always equal to `'development'`, when you run `npm test` it is always equal to `'test'`, and when you run `npm run build` to make a production bundle, it is always equal to `'production'`. **You cannot override `NODE_ENV` manually.** This prevents developers from accidentally deploying a slow development build to production.
21
22
22
-
These environment variables can be useful for displaying information conditionally based on where the project is
23
-
deployed or consuming sensitive data that lives outside of version control.
23
+
These environment variables can be useful for displaying information conditionally based on where the project is deployed or consuming sensitive data that lives outside of version control.
24
24
25
-
First, you need to have environment variables defined. For example, let’s say you wanted to consume a secret defined
26
-
in the environment inside a `<form>`:
25
+
First, you need to have environment variables defined. For example, let’s say you wanted to consume an environment variable inside a `<form>`:
27
26
28
27
```jsx
29
28
render() {
30
29
return (
31
30
<div>
32
31
<small>You are running this application in<b>{process.env.NODE_ENV}</b> mode.</small>
During the build, `process.env.REACT_APP_SECRET_CODE` will be replaced with the current value of the `REACT_APP_SECRET_CODE` environment variable. Remember that the `NODE_ENV` variable will be set for you automatically.
40
+
During the build, `process.env.REACT_APP_NOT_SECRET_CODE` will be replaced with the current value of the `REACT_APP_NOT_SECRET_CODE` environment variable. Remember that the `NODE_ENV` variable will be set for you automatically.
42
41
43
42
When you load the app in the browser and inspect the `<input>`, you will see its value set to `abcdef`, and the bold text will show the environment provided when using `npm start`:
44
43
45
44
```html
46
45
<div>
47
46
<small>You are running this application in <b>development</b> mode.</small>
48
-
<form>
49
-
<inputtype="hidden"value="abcdef" />
50
-
</form>
47
+
<form><inputtype="hidden"value="abcdef" /></form>
51
48
</div>
52
49
```
53
50
54
-
The above form is looking for a variable called `REACT_APP_SECRET_CODE` from the environment. In order to consume this
55
-
value, we need to have it defined in the environment. This can be done using two ways: either in your shell or in
56
-
a `.env` file. Both of these ways are described in the next few sections.
51
+
The above form is looking for a variable called `REACT_APP_NOT_SECRET_CODE` from the environment. In order to consume this value, we need to have it defined in the environment. This can be done using two ways: either in your shell or in a `.env` file. Both of these ways are described in the next few sections.
57
52
58
53
Having access to the `NODE_ENV` is also useful for performing actions conditionally:
59
54
@@ -82,27 +77,26 @@ Note that the caveats from the above section apply:
82
77
83
78
## Adding Temporary Environment Variables In Your Shell
84
79
85
-
Defining environment variables can vary between OSes. It’s also important to know that this manner is temporary for the
86
-
life of the shell session.
80
+
Defining environment variables can vary between OSes. It’s also important to know that this manner is temporary for the life of the shell session.
87
81
88
82
### Windows (cmd.exe)
89
83
90
84
```cmd
91
-
set "REACT_APP_SECRET_CODE=abcdef" && npm start
85
+
set "REACT_APP_NOT_SECRET_CODE=abcdef" && npm start
92
86
```
93
87
94
88
(Note: Quotes around the variable assignment are required to avoid a trailing whitespace.)
To define permanent environment variables, create a file called `.env` in the root of your project:
113
107
114
108
```
115
-
REACT_APP_SECRET_CODE=abcdef
109
+
REACT_APP_NOT_SECRET_CODE=abcdef
116
110
```
117
111
118
112
> Note: You must create custom environment variables beginning with `REACT_APP_`. Any other variables except `NODE_ENV` will be ignored to avoid [accidentally exposing a private key on the machine that could have the same name](https://github.com/facebook/create-react-app/issues/865#issuecomment-252199527). Changing any environment variables will require you to restart the development server if it is running.
0 commit comments