-
-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Document multiple build environments via env-cmd
#4071
#4117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ffa82b8
a2b4b54
86eda51
3ea269a
929bf72
6fcf8e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2196,6 +2196,46 @@ If you are not using the HTML5 `pushState` history API or not using client-side | |
|
||
This will make sure that all the asset paths are relative to `index.html`. You will then be able to move your app from `http://mywebsite.com` to `http://mywebsite.com/relativepath` or even `http://mywebsite.com/relative/path` without having to rebuild it. | ||
|
||
#### Building for Multiple Environments | ||
|
||
Applications are generally split between different environments such as staging, production, and development. To allow the app to run in these different environments one must set environment variables to be able to conditionally run different processes depending on the specified environment. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove lines 2201 through 2209. It's explained elsewhere how Create React App handles environment variables and we don't need to cover it again. |
||
|
||
`create-react-app` handles environment variables in a specific way. [This link](https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-custom-environment-variables) explains how it does but the main facts to know in relation to this are: | ||
|
||
1. You cannot override `NODE_ENV`, it is always set to `'production'` | ||
|
||
2. It is mandated that you prefix any custom environment variables with `REACT_APP_` | ||
|
||
- So you cannot depend upon using `NODE_ENV` to set the environment of your application. | ||
|
||
The ideal way to do this is to use `.env` files as you can specify many environment variables simultaneously. This can be done as so: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to something like: "You can specify a new environment by creating a custom env file. For example, to specify config for a staging environment create a file named |
||
|
||
1. Within `.env.staging` you can set your environment variables: | ||
|
||
- `REACT_APP_API_URL=http://api-staging.example.com` | ||
|
||
2. You can use the [env-cmd](https://www.npmjs.com/package/env-cmd) npm package in conjunction with the `.env` file. | ||
|
||
- To install `env-cmd` you can do so either locally or globally: | ||
|
||
- `npm install env-cmd` or `npm install -g env-cmd` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's leave out the option of globally install |
||
|
||
3. Lastly, within your `package.json`: | ||
|
||
```javascript | ||
{ | ||
// ... | ||
"scripts": { | ||
"build:staging": "env-cmd .env.staging npm run build", | ||
// ... | ||
} | ||
// ... | ||
} | ||
``` | ||
Then you can run `npm run build:staging` or whatever other environment you would like to set up. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this to something like "Then you can run |
||
|
||
You may use `.env.production` as the fallback option in this case as `'production'` is the default `NODE_ENV`. | ||
|
||
### [Azure](https://azure.microsoft.com/) | ||
|
||
See [this](https://medium.com/@to_pe/deploying-create-react-app-on-microsoft-azure-c0f6686a4321) blog post on how to deploy your React app to Microsoft Azure. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this to something like "Customizing Environment Variables for a Build"