How can I run a build for only one environment? - reactjs

I am working on a react project, I have several environments, when I run a build for one environment e.g npm run build-sit, the JavaScript files in the build folder contain information about other environments. How do I make sure that the JavaScript files in that build folder only contain information about the environment I am building for.
Here is my scripts in package.json
"scripts": {
"start": "set REACT_APP_ENV=dev && react-scripts start",
"build": "react-scripts build",
"build-test": "set REACT_APP_ENV=test && react-scripts build",
"build-sit": "set REACT_APP_ENV=sit && react-scripts build",
"build-staging": "set REACT_APP_ENV=staging && react-scripts build",
"build-production": "set REACT_APP_ENV=production && react-scripts build",
"build-company1-uat": "set REACT_APP_ENV=company1Uat && react-scripts build",
"build-company2-uat": "set REACT_APP_ENV=company2Uat && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
I have a constants.ts file where I set the urls for the different environments

Related

npm run <script-name> is failing

I want to build React project as per environment like development, staging and production.
below is snapshot of scripts section inside package.json file.
"scripts": {
"start development": "REACT_APP_ENV=development react-scripts start",
"start staging": "REACT_APP_ENV=staging react-scripts start",
"start production": "REACT_APP_ENV=production react-scripts start",
"build development": "REACT_APP_ENV=development react-scripts build",
"build staging": "REACT_APP_ENV=staging react-scripts build",
"build production": "REACT_APP_ENV=production react-scripts build",
"build": "react-scripts build",
"start": "react-scripts start",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
So as per scripts, 'build development' should build project for development environment.
Now as i run following command in project home directory by specifying environment, ( staging builds for staging env)
npm run 'build staging'
below shown error is displayed and command exits
> testapp#0.1.0 build staging
> REACT_APP_ENV=staging react-scripts build
sh: 1: /tmp/build: not found
Interesting thing is that, the command runs perfectly fine on other systems and creates build folder without any issues but it is throwing error on my current system.
All of my systems are running Ubuntu 20.04.4 LTS
Guys any idea about what might be the issue?
Adding cross-env as dependency and changing scripts to use _ instead of space as shown below resolved the issue
"scripts": {
"start_development": "cross-env REACT_APP_ENV=development react-scripts start",
"start_staging": "cross-env REACT_APP_ENV=staging react-scripts start",
"start_production": "cross-env REACT_APP_ENV=production react-scripts start",
"build_development": "cross-env REACT_APP_ENV=development react-scripts build",
"build_staging": "cross-env REACT_APP_ENV=staging react-scripts build",
"build_production": "cross-env REACT_APP_ENV=production react-scripts build",
"build": "react-scripts build",
"start": "react-scripts start",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Now the following command runs on all platforms without any issues
npm run build_staging

How to set Firefox (Dev Edition) as my default browser for npm start in React

I want to use firefox with create-react-app start, but I keep when I follow the advice of adding.
"scripts": {
"start": "BROWSER='firefox' react-scripts start ",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
or (without quotes),
"scripts": {
"start": "BROWSER=firefox react-scripts start ",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
I get the error message:
'BROWSER' is not recognized as an internal or external command, operable program or batch file.
So how would I specify the BROWSER variable in my package.json file? I'm using Windows 10.

How to tell React which .env file to use?

I have a React app that I run with the command:
npm run dev
This launches the app and uses the .env.local file.
I want to be able to configure a file called .env.production that will be used instead of .env.local when I run the command npm run prod.
These are my current scripts:
"scripts": {
"dev": "react-scripts start",
"start": "serve -s build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
How can I do this?
install npmjs.com/package/env-cmd
"scripts": {
"start": "react-scripts start",
"start:local": "env-cmd -f ./.env.local react-scripts start",
"start:Dev": "env-cmd -f ./.env.development react-scripts start",
"start:QA": "env-cmd -f ./.env.test react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Create a new scripts key and use REACT_APP_ENV :
"scripts": {
"dev": "react-scripts start",
"start": "serve -s build",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"prod" : "REACT_APP_ENV=prod npm run dev"
},

how to configure a react app for production

I want to deploy my react app to production (using heroku). When the app is deployed, React dev tools indicates that my app is running in development mode
I pushed the application to heroku : https://lesamisdhubert.herokuapp.com/
I tried to set an environment variable :
heroku config:set NODE_ENV=production
however, when I console.log(process.env.NODE_ENV), It returns development
I also tried npm run build before pushing to production but it hasn't worked
here are my scripts in the package.json file :
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
however console.log('node env --->', process.env.NODE_ENV); returns 'development'
Does anybody knows how I can set my react app into production mode ? Is this a problem with npm run build ?
Thanks a lot, I really don't understand where this can come from ...
I replaced in package.json :
"scripts": {
"dev": "react-scripts start",
"start": "serve -s build",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"heroku-postbuild": "npm run build"
}
I also created a new heroku app and it seems to work
When configuring enviroment vars in heroku you have to append REACT_APP_VARIABLENAME and then set its value otherwise you can't have access to the env variable.
take a look at: https://github.com/mars/create-react-app-buildpack#compile-time-configuration
you can do this in the heroku client

How auto rebuild and reload browser works in create-react-app

hi I am a newbie in reactJS and am learning reactjs recently by kickstarting from the create-react-app and modifiying it . My doubt is that how the create-react-app detects the changes in files and autimatically rebuilds them . I used a normal nodejs + react + webpack app and in that i have to manually specify the npm run build and node start in the package.json like as
"scripts": {
"build": "webpack",
"start": "node server"
},
in case the above for the create-react-app differ as
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "npm run build-css && run-p -ncr watch-css start-js",
"start-js": "react-scripts start",
"build": "run-s -n build-css build-js",
"build-js": "react-scripts build",
"test": "run-s -n build-css test-js",
"test-js": "react-scripts test --env=jsdom",
"build-css": "node-less-chokidar src",
"watch-css": "node-less-chokidar src --watch",
"eject": "react-scripts eject"
}
What makes the create-react-app and a normal node+webpack+react app differ in rebuilding the source ? (Starting the server seems not a deal because once its started it will be runing on a port for both cases . For Server restart both apps needed to kill the process and start again . So building the source is my only concern) . Any help is appreciated .

Resources