|
| 1 | +--- |
| 2 | +id: adding-custom-environment-variables |
| 3 | +title: Adding Custom Environment Variables |
| 4 | +--- |
| 5 | +> Note: this feature is available with `[email protected]` and higher. |
| 6 | +
|
| 7 | +Your project can consume variables declared in your environment as if they were declared locally in your JS files. By |
| 8 | +default you will have `NODE_ENV` defined for you, and any other environment variables starting with |
| 9 | +`REACT_APP_`. |
| 10 | + |
| 11 | +**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](/docs/injecting-data-from-the-server-into-the-page). Alternatively you can rebuild the app on the server anytime you change them. |
| 12 | + |
| 13 | +> 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. |
| 14 | +
|
| 15 | +These environment variables will be defined for you on `process.env`. For example, having an environment |
| 16 | +variable named `REACT_APP_SECRET_CODE` will be exposed in your JS as `process.env.REACT_APP_SECRET_CODE`. |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +These environment variables can be useful for displaying information conditionally based on where the project is |
| 21 | +deployed or consuming sensitive data that lives outside of version control. |
| 22 | + |
| 23 | +First, you need to have environment variables defined. For example, let’s say you wanted to consume a secret defined |
| 24 | +in the environment inside a `<form>`: |
| 25 | + |
| 26 | +```jsx |
| 27 | +render() { |
| 28 | + return ( |
| 29 | + <div> |
| 30 | + <small>You are running this application in <b>{process.env.NODE_ENV}</b> mode.</small> |
| 31 | + <form> |
| 32 | + <input type="hidden" defaultValue={process.env.REACT_APP_SECRET_CODE} /> |
| 33 | + </form> |
| 34 | + </div> |
| 35 | + ); |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +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 | + |
| 41 | +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`: |
| 42 | + |
| 43 | +```html |
| 44 | +<div> |
| 45 | + <small>You are running this application in <b>development</b> mode.</small> |
| 46 | + <form> |
| 47 | + <input type="hidden" value="abcdef" /> |
| 48 | + </form> |
| 49 | +</div> |
| 50 | +``` |
| 51 | + |
| 52 | +The above form is looking for a variable called `REACT_APP_SECRET_CODE` from the environment. In order to consume this |
| 53 | +value, we need to have it defined in the environment. This can be done using two ways: either in your shell or in |
| 54 | +a `.env` file. Both of these ways are described in the next few sections. |
| 55 | + |
| 56 | +Having access to the `NODE_ENV` is also useful for performing actions conditionally: |
| 57 | + |
| 58 | +```js |
| 59 | +if (process.env.NODE_ENV !== 'production') { |
| 60 | + analytics.disable(); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +When you compile the app with `npm run build`, the minification step will strip out this condition, and the resulting bundle will be smaller. |
| 65 | + |
| 66 | +## Referencing Environment Variables in the HTML |
| 67 | + |
| 68 | +> Note: this feature is available with `[email protected]` and higher. |
| 69 | +
|
| 70 | +You can also access the environment variables starting with `REACT_APP_` in the `public/index.html`. For example: |
| 71 | + |
| 72 | +```html |
| 73 | +<title>%REACT_APP_WEBSITE_NAME%</title> |
| 74 | +``` |
| 75 | + |
| 76 | +Note that the caveats from the above section apply: |
| 77 | + |
| 78 | +- Apart from a few built-in variables (`NODE_ENV` and `PUBLIC_URL`), variable names must start with `REACT_APP_` to work. |
| 79 | +- The environment variables are injected at build time. If you need to inject them at runtime, [follow this approach instead](/docs/generating-dynamic-meta-tags-on-the-server). |
| 80 | + |
| 81 | +## Adding Temporary Environment Variables In Your Shell |
| 82 | + |
| 83 | +Defining environment variables can vary between OSes. It’s also important to know that this manner is temporary for the |
| 84 | +life of the shell session. |
| 85 | + |
| 86 | +### Windows (cmd.exe) |
| 87 | + |
| 88 | +```cmd |
| 89 | +set "REACT_APP_SECRET_CODE=abcdef" && npm start |
| 90 | +``` |
| 91 | + |
| 92 | +(Note: Quotes around the variable assignment are required to avoid a trailing whitespace.) |
| 93 | + |
| 94 | +### Windows (Powershell) |
| 95 | + |
| 96 | +```Powershell |
| 97 | +($env:REACT_APP_SECRET_CODE = "abcdef") -and (npm start) |
| 98 | +``` |
| 99 | + |
| 100 | +### Linux, macOS (Bash) |
| 101 | + |
| 102 | +```bash |
| 103 | +REACT_APP_SECRET_CODE=abcdef npm start |
| 104 | +``` |
| 105 | + |
| 106 | +## Adding Development Environment Variables In `.env` |
| 107 | + |
| 108 | +> Note: this feature is available with `[email protected]` and higher. |
| 109 | +
|
| 110 | +To define permanent environment variables, create a file called `.env` in the root of your project: |
| 111 | + |
| 112 | +``` |
| 113 | +REACT_APP_SECRET_CODE=abcdef |
| 114 | +``` |
| 115 | + |
| 116 | +> 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. |
| 117 | +
|
| 118 | +`.env` files **should be** checked into source control (with the exclusion of `.env*.local`). |
| 119 | + |
| 120 | +### What other `.env` files can be used? |
| 121 | + |
| 122 | +> Note: this feature is **available with `[email protected]` and higher**. |
| 123 | +
|
| 124 | +- `.env`: Default. |
| 125 | +- `.env.local`: Local overrides. **This file is loaded for all environments except test.** |
| 126 | +- `.env.development`, `.env.test`, `.env.production`: Environment-specific settings. |
| 127 | +- `.env.development.local`, `.env.test.local`, `.env.production.local`: Local overrides of environment-specific settings. |
| 128 | + |
| 129 | +Files on the left have more priority than files on the right: |
| 130 | + |
| 131 | +- `npm start`: `.env.development.local`, `.env.development`, `.env.local`, `.env` |
| 132 | +- `npm run build`: `.env.production.local`, `.env.production`, `.env.local`, `.env` |
| 133 | +- `npm test`: `.env.test.local`, `.env.test`, `.env` (note `.env.local` is missing) |
| 134 | + |
| 135 | +These variables will act as the defaults if the machine does not explicitly set them.<br> |
| 136 | +Please refer to the [dotenv documentation](https://github.com/motdotla/dotenv) for more details. |
| 137 | + |
| 138 | +> Note: If you are defining environment variables for development, your CI and/or hosting platform will most likely need |
| 139 | +> these defined as well. Consult their documentation how to do this. For example, see the documentation for [Travis CI](https://docs.travis-ci.com/user/environment-variables/) or [Heroku](https://devcenter.heroku.com/articles/config-vars). |
| 140 | +
|
| 141 | +### Expanding Environment Variables In `.env` |
| 142 | + |
| 143 | +> Note: this feature is available with `[email protected]` and higher. |
| 144 | +
|
| 145 | +Expand variables already on your machine for use in your `.env` file (using [dotenv-expand](https://github.com/motdotla/dotenv-expand)). |
| 146 | + |
| 147 | +For example, to get the environment variable `npm_package_version`: |
| 148 | + |
| 149 | +``` |
| 150 | +REACT_APP_VERSION=$npm_package_version |
| 151 | +# also works: |
| 152 | +# REACT_APP_VERSION=${npm_package_version} |
| 153 | +``` |
| 154 | + |
| 155 | +Or expand variables local to the current `.env` file: |
| 156 | + |
| 157 | +``` |
| 158 | +DOMAIN=www.example.com |
| 159 | +REACT_APP_FOO=$DOMAIN/foo |
| 160 | +REACT_APP_BAR=$DOMAIN/bar |
| 161 | +``` |
0 commit comments