Reload the app when .env file is edited
Description
The environment variables are not being loaded correctly
Expected behavior
I want to use process.env + .env file to load my environment variables.
Actual behavior
I'm trying to use process.env but it have just an empty Object.
The NODE_ENV doesn't exist either.
Environment
Run these commands in the project folder and fill in their results:
npm ls react-scripts(if you haven’t ejected): └── [email protected]node -v: v6.7.0npm -v: 3.10.8
Then, specify:
- Operating system: Windows 10 Pro
- Browser and version: Chrome 53.0.2785.143 m
Having the same problem on macOS Sierra, same version of Chrome.
@mikefrancis I don't know why, but I have closed the cmd (terminal for Linux/OSX) and reopened and it started working.
Just to warn you, when you make changes to the .env file you have to stop the application and run npm start again.
Hope this helps you and another person in the future.
@furlanrapha You're right - when you restart the server these seem to get picked up. Thanks for that!
We should at least try to fix this.
@gaearon I don't know if this was an issue with my PC or with react-scripts, right now is working fine here.
If you guys can add some bind to the .env file so: when we update it, it already reloads the application with the new environment variables (this would be great). But for me it makes sense to stop the app and run npm start again after changing the env vars.
If you guys can add some bind to the .env file so: when we update it, it already reloads the application with the new environment variables (this would be great)
This is the behavior I’d like to see.
I agree with @furlanrapha that manually restart the server might makes more sense, since env vars can affect the build setup, so we will need to re-compile the app from scratch, which is usually a long process.
It is not convenient but I think it is the correct way to think of env variables, as a special configurations that should not be watched continuously. It might also help clarify that env vars replacement happens at init compilation time. Some people can misunderstanding this.
☝️ I've done a PR for the docs to highlight this behaviour just in case it catches anyone else out.
@gaearon how about detecting the changes using nodemon and restart the dev server programmatically? Is this acceptable? here's the reference https://github.com/webpack/webpack-dev-server/issues/440#issuecomment-205757892
I believe it's possible to watch changes like Jest does and trigger update to the WebpackDevServer instance.
This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 7 days if no further activity occurs.