Building a react boilerplate from scratch without using create-react-app - GeeksforGeeks

24 Sep, 2021
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 configurations
Also, 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.
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.
simranarora5sos
Blogathon-2021
React-Questions
Blogathon
ReactJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Import JSON Data into SQL Server?
How to Install Tkinter in Windows?
SQL Query to Convert Datetime to Date
How to pass data into table from a form using React Components
SQL Query to Create Table With a Primary Key
How to fetch data from an API in ReactJS ?
How to redirect to another page in ReactJS ?
How to pass data from child component to its parent in ReactJS ?
Create a Responsive Navbar using ReactJS
How to pass data from one component to other component in ReactJS ?