.env files is not working with CRA 5
See original GitHub issueDescribe 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
npx create-react-app my-app --template typescript
- Add .env file in the project root with the line
REACT_APP_FOO=foo
- Try to access
process.env.REACT_APP_FOO
(or justprocess
itself) somehow. I, for ex, addedconsole.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:
Actual behavior
I got Unhandled Exception, process is undefined:
Reproducible demo
Issue Analytics
- State:
- Created 2 years ago
- Reactions:24
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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: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.