Building a react boilerplate from scratch without using create-react-app
Last Updated :
25 Jul, 2024
In this article, we are going to build a basic boilerplate for a React project from scratch without using the create-react-app or any other predefined boilerplate. This is a great experience for any react developer to look into what is happening behind the scenes.
The file structure for our project will be looking like the following. You will understand how this project structure is created while going through the below steps.

Below is the step-by-step procedure that we will be going to follow.
Step 1: Create an empty new directory and name it according to your choice. Open up the terminal inside that directory and initialize the package.json file by writing the following command:
npm init -y
Here, -y is a flag that creates a new package.json file with default configurations. You can change these default configurations anytime in the package.json file. Package.json contains all dependencies and devDependencies.
A package.json file is created with default configurationsAlso, initialize git in your project if you want. Run the following command on the terminal:
git init
Add a .gitignore file in the root directory and add node_modules in it because node_modules contains all dependency folders and files so the folder becomes too big. Hence, it is not recommended to add it in git.
Step 2: Make two directories named "public" and "src" inside the root directory ( "/"). "public" folder contains all static assets like images, svgs, etc. and an index.html file where the react will render our app while "src" folder contains the whole source code.
Inside the public folder, make a file named index.html.
Filename: index.html
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Basic Boilerplate of React</title>
</head>
<body>
<!-- This is the div where React
will render our app -->
<div id="root"></div>
<noscript>
Please enable javascript to view this site.
</noscript>
<script src="../dist/bundle.js"></script>
</body>
</html>
Step 3: We will write our code in modern ES6 syntax but many browsers do not support it. So, we install Babel that performs the following things:
- Converts new ES6 syntaxes into browser compatible syntaxes so that old versions of browsers can also support our code.
- Converts JSX (JavaScript XML) into vanilla javascript.
To install Babel, run the following command on the terminal:
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-react
Here,
- --save-dev means save all above installed modules in devDependencies in package.json file,
- @babel/core is a module that contains the main functionality of Babel,
- @babel/cli is a module that allows us to use babel from the terminal,
- @babel/preset-env is preset that handles the transformation of ES6 syntax into common javascript,
- @babel/preset-react is preset which deals with JSX and converts it into vanilla javascript.
Now, create a file ".babelrc" in the root directory. This file will tell babel transpiler what presets and plugins to use to transpile the code. Add the following JSON code:
{
"presets": ["@babel/preset-env","@babel/preset-react"]
}
Step 4: Install React and React DOM by running the following command on the terminal:
npm i react react-dom
present inside package.json file.Step 5: Now, create three files inside "src" directory named as 'App.js', 'index.js', 'App.css'. These files contain the actual code.
App.js: A component of React.
JavaScript
import React from "react";
import "./App.css";
const App = () => {
return (
<div>
<h1 className="heading">GeeksForGeeks</h1>
<h4 className="sub-heading">
A computer science portal for geeks
</h4>
</div>
);
};
export default App;
App.css: Provides stylings for App component.
CSS
/* stylings for App component */
.heading,.sub-heading{
color:green;
text-align: center;
}
index.js: Renders the components on the browser.
JavaScript
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(<App/>,document.getElementById("root"));
Note: You can make as many components as you want in your react project inside the src folder.
Step 6: Install the webpack. The webpack is a static module bundler. It works well with babel. It creates a local development server for our project. The webpack collects all the modules (either custom that we created or installed through NPM ) and bundles them up together in a single file or more files (static assets). To install webpack, run the following command on the terminal:
npm install --save-dev webpack webpack-cli webpack-dev-server
Here,
- --save-dev is the same as discussed above,
- webpack is a modular bundler,
- webpack-cli allows us to use webpack from the terminal by running a set of commands,
- webpack-dev-server provides a development server with live reloading i.e. you do not need to refresh the page manually.
The webpack takes code from the src directory and perform required operations like bundling of code, conversion of ES6 syntax and JSX syntax into common javascript etc. and host the public directory so that we can view our app in the browser.
Step 7: Webpack can understand JavaScript and JSON files only. So, to use webpack functionality in other files like .css, babel files, etc., we have to install some loaders in the project by writing the following command on the terminal:
npm i --save-dev style-loader css-loader babel-loader
Here,
- css-loader collects CSS from all the CSS files in the app and bundle it into one file,
- style-loader puts all stylings inside <style> tag in index.html file present in the public folder,
- babel-loader is a package that allows the transpiling of javascript files using babel and webpack.
Step 8: Create a webpack.config.js file in the root directory that helps us to define what exactly the webpack should do with our source code. We will specify the entry point from where the webpack should start bundling, the output point that is where it should output the bundles and assets, plugins, etc.
webpack.config.js
JavaScript
const path = require("path");
module.exports = {
// Entry point that indicates where
// should the webpack starts bundling
entry: "./src/index.js",
mode: "development",
module: {
rules: [
{
test: /\.(js|jsx)$/, // checks for .js or .jsx files
exclude: /(node_modules)/,
loader: "babel-loader",
options: { presets: ["@babel/env"] },
},
{
test: /\.css$/, //checks for .css files
use: ["style-loader", "css-loader"],
},
],
},
// Options for resolving module requests
// extensions that are used
resolve: { extensions: ["*", ".js", ".jsx"] },
// Output point is where webpack should
// output the bundles and assets
output: {
path: path.resolve(__dirname, "dist/"),
publicPath: "/dist/",
filename: "bundle.js",
},
};
Step 9: Now, add some scripts in the package.json file to run and build the project.
"scripts": {
"start":"npx webpack-dev-server --mode development --open --hot",
"build":"npx webpack --mode production",
}
Here,
- --open flag tells the webpack-dev-server to open the browser instantly after the server had been started.
- --hot flag enables webpack's Hot Module Replacement feature. It only updates what's changed in the code, so does not update the whole code, again and again, that's why it saves precious development time.
Step to run the application: Run the command following on the terminal to run the project in development mode.
npm start
Output:
Run command "npm run build" to run the project in production mode.
Note: When we are running our webpack server, there isn't a dist folder. This is because what webpack server does is holds this dist folder in the memory and serves it, and deletes it when we stop the server. If you actually want to build the react app so that we can see that dist folder, run the command "npm run build". Now, you can see the dist folder in the root directory.

That's all! We are equipped with our own react boilerplate and ready to make some amazing and cool projects.
Similar Reads
Building a Component Library with React Hooks and Storybook Building a component library is a powerful way to streamline the development process, ensure consistency across projects, and promote the reusability of UI elements. In this article, we'll explore how to create a component library using React Hooks and Storybook, a tool for developing UI components
3 min read
How to Migrate from create-react-app to Vite? In this article, we will learn how we can migrate from create-react-app to Vite. Vite is a build tool for frontend development that aims for fast and efficient development. It provides a development server with hot module replacement and supports various JavaScript flavors, including JSX, out of the
3 min read
How to Add Express to React App using Gulp ? When building a web application with ReactJS, you may need to include a backend server for handling server-side logic and API requests. Express is a popular choice for creating backend servers in Node.js. In this tutorial, we'll explore how to integrate Express with a React app using Gulp as the bui
3 min read
How to Create Form Builder using React and Dynamic State Management ? In web development building forms is a common task. However, creating dynamic forms with varying fields and configurations can be a challenge. This is where a form builder tool comes in handy. In this article, we'll learn how to build a form builder tool using React Hooks for state management, allow
3 min read
Create an Agency Website using React and Tailwind An agency website using React and Tailwind consists of basic components such as a home page, services, contact us, features, etc. It also has a contact form that will enable the user to contact if any issue arises on the website or the benefits to be availed by the user through the website. Preview
6 min read
What are the Issues of Continuing to Import createRoot from "react-dom"? In the world of web development, React is a popular library for building user interfaces. React has been continuously evolving, and with each new version, it brings better ways to build and manage applications. One of the changes in the recent versions of React is the way we create the root of our a
3 min read
Build a Basic React App that Display âHello World!â React is a Javascript Library that was created by Facebook for building better User Interface(UI) web applications and mobile applications. It is an open-source library for creating interactive and dynamic applications. In this article, we will see how to build a basic react app that shows Hello Wor
1 min read
How To Create A Multi-Page Website Using ReactJS? Multi-page website using ReactJS is the core for building complex web applications that require multiple views or pages. Previously where each page reloads entirely, ReactJS allows you to navigate between different pages without reloading the whole page thanks to libraries like React Router.In this
4 min read
How to build and publish an NPM package for React using Typescript? In development, creating and distributing reusable components is essential for building scalable and maintainable applications. With the popularity of TypeScript and React, you can easily package and share your components as NPM packages. This tutorial will teach us how to create and release our NPM
4 min read
How to add Stateful component without constructor class in React? Generally, we set the initial state of the component inside the constructor class and change the state using the setState method. In React basically, we write HTML-looking code called JSX. JSX is not a valid JavaScript code but to make the developer's life easier BABEL takes all the responsibility t
2 min read