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

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 .

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

Run two scripts from package.json in Netifly

I have deployed my React app successfully in Netifly.
My app live URL
Here is my package.json file's scripts:
"scripts": {
"start": "react-scripts start",
"json-server": "json-server --watch db.json --port 3003",
"start:dev": "concurrently \"npm start\" \"npm run json-server\"",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
But the problem is: the site unable to show any kind of data. Please visit to have an idea. Because, json-server as backend serves db.json. It's not running in production.
Locally i can run yarn start:dev but couldn't able to run both scripts concurrently. What will be the proper scripts to run both react-scripts start && json-server --watch db.json --port 3003 in production?
I found myself the solution: "start": "concurrently \"react-scripts start\" \"npm run json-server\"", by updating solves the problem.

What is the difference between npm start and serve s build?

I know two way to deploy server is npm start and serve s build
So what different between npm start and serve s build to start server in react js?
These are all custom script, you can find the definition in package.json file. Normally start is for starting the development build, build is for making the production build.
Ex.
"scripts": {
"start": "react-scripts start",
"lint": "eslint ./src",
"lint:fix": "eslint ./src --fix",
"test": "react-scripts test",
"test:coverage": "react-scripts test --coverage --watchAll=false",
"build": "rollup -c",

Blank page on create-react-app when deploy to netlify

I'm trying to deploy create-react-app(which is currently on github) to netlify. after it deployed it shows a blank page.
Netlify configurations i have added:
build command: yarn build,
Publish directory: build
and this is my scripts on package.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
I was getting blank page only on mobile devices after deploying on Netlify. It worked after I removed REDUX DEVTOOLS EXTENSION from Redux store.

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

Resources