question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.env files is not working with CRA 5

See original GitHub issue

Describe the bug

I created totally fresh CRA5 project (TypeScript), except .env added in the project root and src/App.tsx is modified with attempt to access process.env variables. I got crash with unhandled exception process is undefined. I think it’s somehow related to Webpack 5 upgrade.

Did you try recovering your dependencies?

No, because project is fresh create-react-app v5 bootstrap.

Which terms did you search for in User Guide?

https://create-react-app.dev/docs/adding-custom-environment-variables/ said everything is supposed to work fine.

Environment

Environment Info:

  current version of create-react-app: 5.0.0
  running from /Users/evgenijvladimirovic/.npm/_npx/33863/lib/node_modules/create-react-app

  System:
    OS: macOS 12.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 14.16.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.11 - /usr/local/bin/npm
  Browsers:
    Chrome: 97.0.4692.71
    Edge: Not Found
    Firefox: 94.0.2
    Safari: 15.2
  npmPackages:
    react: ^17.0.2 => 17.0.2 
    react-dom: ^17.0.2 => 17.0.2 
    react-scripts: 5.0.0 => 5.0.0 
  npmGlobalPackages:
    create-react-app: Not Found

Steps to reproduce

  1. npx create-react-app my-app --template typescript
  2. Add .env file in the project root with the line REACT_APP_FOO=foo
  3. Try to access process.env.REACT_APP_FOO (or just process itself) somehow. I, for ex, added console.log("process is ", process); and {process.env.REACT_APP_FOO} ini JSX.

Expected behavior

I got the REACT_APP_FOO value from .env file, just like in CRA Docs: image image

Actual behavior

I got Unhandled Exception, process is undefined: image

Reproducible demo

https://github.com/ru-web-designer/cra5-process-env-issue

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:24
  • Comments:7

github_iconTop GitHub Comments

3reactions
kitsunekyocommented, Jan 18, 2022

we also found that CRA5 is almost unusable without ejecting at the moment.

we ejected, installed process and added this line to the webpack config:

resolve: {
      // ...
      alias: {
        // ...
        process: 'process/browser',
      },
3reactions
pganstercommented, Jan 18, 2022

With CRA 5, webpack was upgraded to v5, where node built-ins (such as process) have been removed.

You either need to eject your app which isn’t desirable or use something like craco to configure webpack with an unejected create-react-app.

There is an open pull request that should address this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

create react app not picking up .env files? - Stack Overflow
in my case I was copy file from different OS and use in ubuntu system so just I did "sudo touch .env" and...
Read more >
Adding Custom Environment Variables | Create React App
Environment variables are embedded into the build, meaning anyone can view them by inspecting your app's files. The environment variables are ...
Read more >
Create-react-app environments - Medium
env files, variables are not global to the environment and will not persist beyond the running of the start command. In my opinion,...
Read more >
Can't Read Environment Variable In React?
To define Environment Variable, open the .env file and paste the following code: ... Our variable name is definitely wrong and will not...
Read more >
kunokdev/cra-runtime-environment-variables: Guide - Ainize.ai
The problem. First of all, it must be clear that there is no such thing as environment variables inside browser environment. Whichever solution...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found