Create React App without create-react-app !
Tags: javascript, react, webdev, tutorial
Published: 2021-08-13 10:22:40

The simplest way to create a basic react app is to run npx create-react-app and boom your basic react app will be created but have you ever wondered, can I do that whole process on my own? Well yes, you can. 

**Pre-requisites**: Node js and Vs code. That’s all you need.

Let’s do it ..!!!
![Yeayyy..!!](https://c.tenor.com/Jw0RZGetgwIAAAAC/minions-yay.gif)

###1. Open your vs code terminal and run the below commands:###

```js 
npm init -y
```
By running this command package.json will be formed which holds important information which is required before publishing to NPM, and also defines attributes of a project that is used by npm to install dependencies, run scripts, and identify the entry point of the project. 

   
```js
npm install react react-dom 
```


React will be needed to create user interfaces whereas React-Dom is a glue between React and browser DOM.

After running this command, node_modules and package.lock.json will be created. Node modules hold all the dependencies downloaded from npm. Package.lock.json keeps track of the exact version of every package that is installed as well as the dependency tree of every package.  

```js
npm install --save-dev @babel/core @babel/preset-env @babel/preset-react babel-loader
```
Babel is a JS Compiler that converts modern JS code into vanilla Js code that can be supported in older browsers and environments. Babel-loader transpile JS files using Babel and webpack.
For further reading visit https://babeljs.io/docs/en/

###2.Create a file .babelrc and copy the code below###
```js
{  
 "presets": [
        "@babel/preset-react",
        "@babel/preset-env"
            ]
}
```

This file is a configuration file for babel whereas presets act as a shareable set of Babel plugins and/or config options.

```js
npm install --save-dev webpack webpack-cli webpack-dev-server
```

Webpack is a tool that lets you compile JavaScript modules, also known as module bundlers.Given a large number of files, it generates a single file (or a few files) that run your app. Webpack-CLI provides the interface of options webpack uses in its configuration file.

```js
npm install --save-dev html-webpack-plugin style-loader css-loader file-loader
```

All these are loaders that help in bundling various files along with webpack.  
###3.Create a file webpack.config.js and copy the code below###

```js
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "bundle.[hash].js",
    path: path.resolve(__dirname, "dist"),
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html",
    }),
  ],
  resolve: {
    modules: [__dirname, "src", "node_modules"],
    extensions: ["*", ".js", ".jsx", ".tsx", ".ts"],
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: require.resolve("babel-loader"),
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.png|svg|jpg|gif$/,
        use: ["file-loader"],
      }, 
    ],
  },
};
```

This config file provides all the required information like an entry point,bundle output filename and path , plugins and various loaders that are being used for webpack to bundle and resolve various kinds of files.
For further reading visit: https://webpack.js.org/concepts/


###4.Create a folder “src” and inside that create a file “App.js”###
```js
import React from "react";

const App = () => ( 
    <div>
        <h1>Hello React</h1>
    </div>
);
 
export default App;
```   
This is a basic arrow function that returns  Hello React wrapped inside an h1 tag.

###5.Create a file “index.js” that will be the entry point of our code.###
```js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
 
ReactDOM.render(<App/>,document.querySelector("#root"));
```   


###6.Create another file “index.html”###
```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>React</title>
</head>

<body>
    <div id="root"></div>
</body>

</html>
```


In our configuration, we specified that it should read ./src/index.HTML as a template. We have also set the inject option to true. With that option, Html-webpack-plugin adds a script with the path provided by Webpack right into the final HTML page. This final page is the one you get in dist/index.html after running npm run build, and the one that gets served from / when you run npm start.

###7.In your package.json write the following lines of code in place of the script tag###

```js
"scripts": {
    "start": "webpack serve  --hot --open",
    "build": "webpack --config webpack.config.js --mode production"
  }
```

You can start your react app by writing npm start and you will see a blank page with Hello React written on it.

![Final Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n608fvjpum3d5e065b57.png)

##And you are done ..!!
Now you can customize your React app and add various components to it .

Though it was quite a long process and that’s what create-react-app makes it easier for you. It automates this whole hefty process of making every single file by replacing it with just a single command npx create-react-app filename.