Why it is better to avoid Create React App (CRA) CRA was created for beginners (according to Facebook) to learn React without understanding the underline configurations of Webpack. However, it is not created for production. Unfortunately, devs have been using it for all their projects.
Create React App is a comfortable environment for learning React, and is the best way to start building a new single-page application in React. npx on the first line is not a typo — it's a package runner tool that comes with npm 5.2+.
If you really need create-react-app , you might need to reinstall Node and reconfigure your dependencies to ensure you have a fresh start with Node, npm, npx, and the like.
The best option is probably to do a normal production build and then run it locally.
First install an HTTP server:
npm install serve -g
Then:
npm run build
serve -s build
By default, it will run on port 5000 so your local URL is http://localhost:5000
To serve the app in production mode you need to follow below steps
create a production build
npm run build
install npm serve globally
npm i -g serve
You may serve it with a static server now
serve -s build
You can check for more options here.
For the development in production you can enable Hot reloading in react app without ejecting
With just 3 lines of code, you can turn on HMR, but with one big caveat: React state and DOM state are not preserved between reloads. This is kind of a bummer.
You can add these lines to index.js to turn on standard Webpack HMR that doesn’t preserve state between reloads:
if (module.hot) {
module.hot.accept();
}
Read more about it here Hope it helps Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With