How to use .env in react js - reactjs

How to use .env in react . I can get access to .env.development and .env.production with
"start":"react-scripts start",
"build": "react-scripts build",
How to get access to another like .env.staging ?
I gave like this
"build_staging": "set REACT_APP_ENV=staging & react-scripts build",
but not working.
Any suggestions please.

To keep things consistent across linux(my production server) and windows(my development server) I use cross-env
npm install --save cross-env
and my scripts look like this
"scripts": {
"dev": "cross-env NODE_ENV=development node server",
"build": "cross-env NODE_ENV=production next build ",
"start": "cross-env NODE_ENV=production node server"
},
so to set a custom env like REACT_APP_ENV you'll need to
"build_staging": "cross-env REACT_APP_ENV=staging react-scripts build",
and you can access it in your javascript code using
process.env.REACT_APP_ENV
also to start a staging server you might want to add
"start_staging": "cross-env REACT_APP_ENV=staging react-scripts start"
more about this here

[CONTEXT]
- I've created React project with Create React.
- Running on Windows 10
- Using VSCode IDE
- I have the file .env.development on above /src folder.
- My code has console.log(process.NODE_ENV) and console.log(process.REACT_APP_YOUR_KEY)
[PROBLEM]
When I'm running the program with npm start, the browser prints 'development' for NODE_ENV, but for my React .env variable, it prints undefined.
I try to run npm start with changing the default script of package.json to this start script: set REACT_APP_YOUR_KEY && react-scripts start. Then there isn't any undefined, all works well. 🤨
[SOLUTION]
The cause was that the file .env.development is not detected correctly. My hypothesis is the environment development file, Windows or VSCode don't detect well.
Windows File Explorer files
VS Code explorer. Look up the icon of the files 🤔
You have to change the file name from .env.development to .env.

[SOLUTION]: I've created env.js as shown below.
env.js placement
after that i have added in index.html the script below
my env.js content
NB: i can acces my env.js var without import them (it seems the best way for me)

Related

GENERATE_SOURCEMAP=false not working on heroku

I deployed my react app on heroku and chose github as deployment method. To hide the source code, I added GENERATE_SOURCEMAP=false in a .env but it didn't work. In package.json also I tried adding
"build": "set \"GENERATE_SOURCEMAP=false\" && react-scripts build" but it didn't work. I also added it in Config Vars in the heroku project settings.
Please help!
If you are using linux adding it to the package.json will do the trick:
"scripts": {
...
"build": "GENERATE_SOURCEMAP=false react-scripts build"
},

Create-React-App .env file directory location (Non-root)

After creating a new web app with create-react-app (CRA), I need to include some environment files for configuring various endpoints. Noticed that CRA comes with the cool dotenv package all ready to go. There's only one problem with that - I would like to have dotenv read these files from within my ./environments directory and not the root directory. Is there any way to load the .env, .env.local, .env.test, etc... files in a directory separate from the root directory?
Noticing I can achieve this in my express backend server.js by simply importing like so:
require('dotenv').config({ path: `./environments/` })
Can I do the same with my client-side code in React? If so, where should I put this import? Doesn't seem to work for me.
If you created your project using CRA and want to configure dotenv to change the path from where it loads the env files, you will have to do npm eject.
Alternatively, you can use env-cmd to achieve the same:
Installation:
npm i env-cmd
Add scripts in package.json, for example:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"dev": "env-cmd -f envs/.env.dev react-scripts build",
"qa": "env-cmd -f envs/.env.qa react-scripts build",
"demo": "env-cmd -f envs/.env.demo react-scripts build"
},
After creating .env.dev, .env.qa, .env.demo in envs/ directory.
Now, you can run:
$ npm run dev // it will use envs/.env.dev file
$ npm run qa // it will use envs/.env.qa file
$ npm run demo // it will use envs/.env.demo file

Set env Variables in react project

Someone please help me with below
i have installed npm i env-cmd
i have 2 files in the src folder
.env.development
REACT_APP_URL="DEVELOPMENT"
.env.production
REACT_APP_NAME="PRODUCTION"
package.json file
"start": "env-cmd .env.development react-scripts start",
"build": "env-cmd .env.production react-scripts build",
But i get nothing while do this in app.jsx
{process.env.REACT_APP_NAME}
i also dont get any error in cmd
well you have REACT_APP_URL in dev but you're trying to access REACT_APP_NAME so of course it doesn't work
change this: REACT_APP_URL="DEVELOPMENT" to REACT_APP_NAME="DEVELOPMENT and it should work

Deploy env file in test and qa

For setting the API URL and key value , i have created .env.development and .env.qa for test and QA environment. But how to deploy this file in azure devops,like how to setup the environemtn variable in pipeline.
can someone guide me through steps.
We have a similar setup, multiple dotenv files for each environment. In the package.json, we've defined multiple build scripts:
"scripts": {
"build": "react-scripts build",
"build:test": "env-cmd --no-override .env.tst react-scripts build",
"build:qa": "env-cmd --no-override .env.qa react-scripts build",
"build:prod": "env-cmd --no-override .env.prod react-scripts build"
},
Don't use .env.production or .env.test however, otherwise, these will be used automatically by react-scripts in every build or test command.
I have successfully implemented the feature in Azure Devops.
I have created seperate zip file by creating new task for test and qa,buildid with the suffix mentioning the environment in the Build Pipeline. Similarly we need to mention the given zip folder name in Test and QA task in release pipeline.

How to create and run a development build of an application using create-react-app configuration

npm run build creates production build of the project. How do I create development build? I'm using gradle-node-plugin to build the artifact. The project has standard create-react-app configuration.
This is not really doable with just create-react-app, there is an open issue Here and it doesn't look like it will be added anytime soon.
However, you can use a package called dotenv for that, following are the steps you should take:
Install dotenv (make sure to add save dev) npm install dotenv-cli --save-dev
In package.json scripts section add new script: "build:dev": "dotenv -e .env.development react-scripts build",
And you can build for development with npm run build:dev
PS: if you want to avoid mistakenly deploying dev builds to production (as mentioned here) you can add build:prod to package.json and disable the regular build command, see code:
"build:dev": "dotenv -e .env.development react-scripts build",
"build:prod": "dotenv -e .env.production react-scripts build",
"build": "echo \"Please use build:dev or build:prod \" && exit 1",
Also note that process.env.NODE_ENV will still be production but it'll load your .env.development file
Thanks, #Moses. This is an extension to the answer posted by Moses Schwartz. You can also make the build pick the environment files dynamically by exporting the value of the environment(development, test, production) in the bash shell. And then you don't have to have different build commands for different environments.
You can use this in your package.json
"start": "dotenv -e .env react-scripts start",
"build": "dotenv -e .env.${REACT_APP_ENVIRONMENT} react-scripts build",
So when your run npm start, it will pick the environment values from .env
and when you run npm run build, it will pick the environment values from .env.{REACT_APP_ENVIRONMENT}
To define the REACT_APP_ENVIRONMENT, you can do:
export REACT_APP_ENVIRONMENT="development" or
export REACT_APP_ENVIRONMENT="test" or
export REACT_APP_ENVIRONMENT="production"
Hope this helps. This will help in staging the react application for multiple environments.
Thanks to #Tx_monster comment
github.com/Nargonath/cra-build-watch
A script for create-react-app that writes development builds to the disk
npm install -D cra-build-watch
package.json:
{
"scripts": {
"watch": "cra-build-watch"
}
}
npm run watch

Resources