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
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.React.jsWhy Use React?Before React, web development faced issues like slow DOM updates
7 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides methods to interact with the Document Object Model, or DOM. This package allows developers to access and modify the DOM. It is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient wa
3 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsReact Lists are used to display a collection of similar data items like an array of objects and menu items. It allows us to dynamically render the array elements and display repetitive data.Rendering List in ReactTo render a list in React, we will use the JavaScript array map() function. We will ite
5 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. In this article, we'll explore ReactJS keys, understand their importance, how the
5 min read
Components in React
React ComponentsIn React, React components are independent, reusable building blocks in a React application that define what gets displayed on the UI. They accept inputs called props and return React elements describing the UI.In this article, we will explore the basics of React components, props, state, and render
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.What are Reactjs Functio
5 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
4 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem. Since in the latest version of the React 19, PropeTypes has been removed. What is ReactJS PropTypes?PropTypes is a tool in React that he
5 min read
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects