facebook / create-react-app

Set up a modern web app by running one command.
https://create-react-app.dev
MIT License
102.71k stars 26.85k forks source link

react-scripts start does not acknowledge changes to env vars in file #13416

Open tyliggity opened 1 year ago

tyliggity commented 1 year ago

Describe the bug

Not sure if this is intended behavior but the live reload functionality behind react-scripts start does not acknowledge any changes to environment variables, at least those provided in a file. I tried with .env.development.local. It's odd that the first time it runs the variables come through but then are never updated on subsequent reloads. I did not see any note of this in the docs and wasted probably 10 minutes before I just killed it and re-ran the same command to discover that was indeed the problem.

Did you try recovering your dependencies?

I was using react-scripts v5.0.1

Which terms did you search for in User Guide?

Environment variable docs: https://create-react-app.dev/docs/adding-custom-environment-variables/#adding-development-environment-variables-in-env

Environment

 current version of create-react-app: 5.0.1
  running from /.../.../.npm/cache/_npx/c67e74de0542c87c/node_modules/create-react-app

  System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
  Binaries:
    Node: 18.14.0 - /usr/local/bin/node
    Yarn: 1.22.19 - ~/.npm-global/bin/yarn
    npm: 9.2.0 - ~/.npm-global/bin/npm
  Browsers:
    Chrome: 118.0.5993.117
    Edge: Not Found
    Safari: 14.0.3
  npmPackages:
    react: ^18.0.0 => 18.2.0 
    react-dom: ^18.0.0 => 18.2.0 
    react-scripts: ^5.0.1 => 5.0.1 
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

(Write your steps here:)

  1. Create .env.development.local at repo root with a value
  2. Run react-scripts start
  3. Change value in above file, wait for reload or refresh manually
  4. Value does not update

Expected behavior

Value should update

Actual behavior

Value does not update

Reproducible demo

(Paste the link to an example project and exact instructions to reproduce the issue.)

pseidemann commented 11 months ago

react-scripts loads the .env* files via dotenv. so dotenv would need to notify the server when it detected env file changes, so the server can reload/restart.

I think this would be a feature request to https://github.com/motdotla/dotenv

but note: native env vars set from your shell (and not via dotenv from .env* files) are always copied to the server process (in fact any process) and thus subsequent changes to them are not visible to it. it is impossible for a process to notice a change to env vars and a restart in this case is always needed