I want to use env variables in a React project CRA-4.0.3 with Typescript.
I did try many approaches(here, here, here , and some other) using dotenv, webpack and suggestions from different sources but I still get undefined.
But, I still get undefined, can someone point me in the right direction? I already spend a reasonable amount of time.
Update: I forgot to mention that I want them to be available in runtime, so, I can use them for E2E Test with Playwright and Jest as runner.
I'm using them in a structure like this /main/e2e/tests/file.spec.ts
.env
REACT_APP_ADMINUSER=user REACT_APP_ADMINPASS=pass
Thanks in advance
DefinePluginsettings if you don't use Create React App. webpack.js.org/plugins/define-plugin This plugin allows you to parse variables in React code. You should define variables that you want to pass in this webpack setting. Create React App provides DefinePlugin from scratch, so it should work out of the box. If you use CRA, please, make sure that you defined.envfile in the root directory (besides package.json). Example:A=1(.env),console.log(process.env.A)(App.jsx)