create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Reload the app when .env file is edited

Open furlanrapha opened this issue 10 years ago • 11 comments

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:

  1. npm ls react-scripts (if you haven’t ejected): └── [email protected]
  2. node -v: v6.7.0
  3. npm -v: 3.10.8

Then, specify:

  1. Operating system: Windows 10 Pro
  2. Browser and version: Chrome 53.0.2785.143 m

furlanrapha avatar Oct 07 '16 14:10 furlanrapha

Having the same problem on macOS Sierra, same version of Chrome.

mikefrancis avatar Oct 07 '16 15:10 mikefrancis

@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 avatar Oct 07 '16 15:10 furlanrapha

@furlanrapha You're right - when you restart the server these seem to get picked up. Thanks for that!

mikefrancis avatar Oct 07 '16 16:10 mikefrancis

We should at least try to fix this.

gaearon avatar Oct 07 '16 19:10 gaearon

@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.

furlanrapha avatar Oct 08 '16 16:10 furlanrapha

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.

gaearon avatar Oct 10 '16 14:10 gaearon

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.

thien-do avatar Oct 14 '16 09:10 thien-do

☝️ I've done a PR for the docs to highlight this behaviour just in case it catches anyone else out.

mikefrancis avatar Oct 14 '16 09:10 mikefrancis

@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

viankakrisna avatar Mar 10 '17 07:03 viankakrisna

I believe it's possible to watch changes like Jest does and trigger update to the WebpackDevServer instance.

miraage avatar Aug 02 '17 15:08 miraage

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.

stale[bot] avatar Nov 02 '18 06:11 stale[bot]