How to tell ReactJS to build project in Production mode ?
In development mode, React includes many warnings to help in finding problems before they lead to bugs. Doing so, it increases the bundle size and makes the app run slower. The slowdown may be accepted while working on the app locally but we cannot afford this in deployment.
In development mode React, internally, uses several clever techniques to minimize the number of costly DOM operations required to update UI. Nevertheless, there are several ways we can speed up our React application – production builds being one of them.
What will production build do?
The production build creates minified bundles, lighter-weight source maps, and optimized assets. This improves the load time. React recommends using production mode while deploying the react app. We now know that production build helps in optimizing performance.
Let’s see now how to create react app in production mode.
Creating React Application using Create React App:
Step 1: Open terminal and run the following command to create project folder of the react application:
npx create-react-app myapp
Step 2: Move into the project folder:
Project Structure: The initial project structure will look like the following.
Step 3: Now edit the App.js file in the project and replace pre-existing code with the following code.
Step to run the application: Go to the root of the project folder and run one of the following commands:
Output: The app will run on port 3000 and the output screen will look like this:
Step 4: Now create the production build of the app. React App makes it quite simple to build in production. If your project is build with create-react-app, go to the root directory of the project and run:
npm run build
Step 5: Running the app in production mode. We will serve the build version with a static server: In production mode the react app will run on port 5000.
serve -s build
Now let’ see how to know whether the build process was setup correctly
1. We will be using React Developer Tools for Chrome . When this extension is installed and running and we visit a site with react in production mode, the react icon will have dark background.
2. If you visit a site with React in development mode, the icon will have a red background: