Skip to content

REACT_APP_ environment variables not replaced for public folder js file during build time #10624

Description

@aisensiy

Describe the bug

I wanna the react app to notify all the browse when there is a new version deployed so that the end user can try to refresh the opened page without any error message with lazy loading.

There is a loop to check a file public/config.js which include the latest app version in it:

window.__env = {
    // other variables ....
    "CURRENT_VERSION": process.env.REACT_APP_VERSION
}

But it seems that the variable process.env.REACT_APP_VERSION is not replaced during the build time?

Did you try recovering your dependencies?

I followed the docs in https://create-react-app.dev/docs/adding-custom-environment-variables#referencing-environment-variables-in-the-html but it seems only work for the public/index.html file.

Which terms did you search for in User Guide?

I think these two are related issues.

#7953
#8004

And #8004 (comment) is mentioned that:

This is fairly easily solved in most cases as a custom script within your project that is run before the react-scripts build.

But why the public/index.html works? I just think the custom script can be too tricky. A general mechanism is necessary.

Environment

Environment Info:

  current version of create-react-app: 4.0.2
  running from /Users/shanchuanxu/.config/yarn/global/node_modules/create-react-app

  System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
  Binaries:
    Node: 12.12.0 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.11.3 - /usr/local/bin/npm
  Browsers:
    Chrome: 88.0.4324.192
    Edge: Not Found
    Firefox: 84.0.2
    Safari: 14.0.3
  npmPackages:
    react: ^16.13.0 => 16.14.0
    react-dom: ^16.13.0 => 16.14.0
    react-scripts: 4.0.1 => 4.0.1
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

(Write your steps here:)

Expected behavior

The env var in public/config.js can be replaced with a string.

Actual behavior

(Write what happened. Please add screenshots!)

Reproducible demo

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions