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.
React.js provides a powerful and efficient build process that optimizes your application for production deployment. By default, React.js builds your project in development mode, which includes features like detailed error messages and debugging tools. However, when you’re ready to deploy your application, it’s important to build it in production mode to benefit from optimized performance and reduced bundle size. In this article, we will explore how to instruct React.js to build your project in production mode.
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:
cd myapp
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.
App.js
Javascript
import logo from './logo.svg' ; import './App.css' ; function App() { return ( <div className= "App" > <header className= "App-header" > <img src={logo} className= "App-logo" alt= "logo" /> <p> This is my React App. </p> </header> </div> ); } export default App; |
Step to run the application: Go to the root of the project folder and run one of the following commands:
npm start
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
This will create a build directory with production build of the app. Your JavaScript and CSS files will be inside the build/static directory. A number of .js files are generated and placed inside the build/static/js directory. These are called chunks.
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: