Say No to Create React App!

Search for a command to run...

No comments yet. Be the first to comment.
💡 “So if you want to go fast, if you want to get done quickly, if you want your code to be easy to write, make it easy to read.” -By Robert C. Martin JavaScript has progressed a ton in recent years. If you are learning JavaScript in 2022 and you ...

Getting Started with Node.js In this blog, we will try to understand the basics of Node.js, how it works, and why to choose Node.js, but before diving into node let's talk a little bit about Java-script. JavaScript is one of the best programming lang...

If you are reading this, you probably know what React.js is and might have already used it Earlier. However, you might be wondering why I am reading about folder structure? Can’t I just stuff all my files in the src folder?? 🤪 Technically you could ...

Let’s first understand 🤔 What is an Array? Quoting from MDN directly. The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name and has members for performing common...

Create React App is a comfortable environment for learning React and is the best way to start building new single-page applications.
CRA or Create React App was built for beginners to learn React without needing to configure WebPack and thus decreasing the entry barrier to create a react app on a whim.
However, this approach is not suitable for production at all. Unfortunately, CRA is being used in far more cases than it should be.
We can look at it in this way say You are a superhero ( Tu hai Tu hai tereko pata nai hai Tu hai 😜)( Remember Batman Begins if you have seen it). You need a suit at the beginning and in hurry, you would use a one-piece suit useful in an instant.
However to add functionalities you would need to add parts to add. You would need to have a lot of flexibility and movement. This is only facilitated if you build the entire suite by yourself assembling each part with inspection and usage.
Create an empty directory name it ( noCreateReactApp) just kidding name it to your choice 😜.
Open the terminal or open the folder in VS Code in the terminal initialize the package.json file using this command.
npm init -y
It contains all the dependencies currently and automatically updates with all dependencies added to the file in the folder.
{
"name": "nocreatereactapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
git init
Also, add a .gitingnore in the root directory and write node_modules in .gitignore. Because it would be a huge folder with no significance added to git without any practical use at all.
Folder structure for React Project. You would have noticed the public and src folders inside the root directory(”/”). We would also be making these two folders in our project.
It contains all the static images,svgs, and index.html file in which React renders the app.
You might have noticed whenever you would use an image file in anchor tags it usually is placed in the public folder and is added using a relative path with respect to the public.
In the public folder let’s add index.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>Not a Create React App</title>
</head>
<body>
<!-- React will render our app here -->
<div id="root"></div>
<noscript>
Please enable javascript to view this site.
</noscript>
<script src="../dist/bundle.js"></script>
</body>
</html>
After initializing an NPM project in our folder, we should install React and React DOM.
npm install react react-dom
Add App.jsx or App.js to the root directory. This will provide content to the rendered page.
Alongside also make an App.css page to give styling to App.jsx.
import React from "react";
import "./App.css";
const App = () => {
return (
<div>
<h1 className="heading">Not a Create React App</h1>
<h4 className="sub-heading">
CSA from scratch.
</h4>
</div>
);
};
export default App;
/* stylings for App component */
.heading,.sub-heading{
color:blueviolet;
text-align: center;
}
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(<App/>,document.getElementById("root"));
The current folder structure looks as such :

.gitignore for git<!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>Not a Create React App</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="../index.jsx"></script>
</body>
</html>
<script> tag is modified to import index.jsx. In future, we will add bundle.js which would bundle all the JS files into one.
But now in order to check if the app is ready for use, we add index.jsx.
We encounter this error! 🙄

So here comes the important steps to add babel and WebPack to our app.
We write our code in modern ES6 syntax but most browsers do not support it. So, in order for our app to run on the browser, we need Babel.
There are two major functions of Babel: -
We install babel/core, babel/cli, babel/present-env, and babel/present-react using this command on the terminal.
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/preset-react
Here,
node_modules
folder. And is a module that allows us to use babel from the terminal,{
"presets": ["@babel/preset-env","@babel/preset-react"]
}
node_modulesnpx babel index.jsx --out-dir lib
This command tells NPX — NPM’s in-built package runner, to run the Babel executable installed in the
node_modulesfolder. Afterbabelthere are the arguments to the Babel executable, which is the source file or directory (index.jsx) and the output file or directory (lib) prepended by the out-dir flag.
The output newly transpiled JS index.js file can be found in lib/folder of root directory.
"use strict";
var _react = _interopRequireDefault(require("react"));
var _reactDom = _interopRequireDefault(require("react-dom"));
var _App = _interopRequireDefault(require("./App"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
_reactDom["default"].render( /*#__PURE__*/_react["default"].createElement(_App["default"], null), document.getElementById("root"));
<script> tag to new index.js in lib folder instaed of index.jsx in root directory.<!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>Not a Create React App</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="../lib/index.js"></script>
</body>
</html>
Instead we get this error in the console.

Install the WebPack.
In brief :
What is WebPack and What are Modules?
Now coming to code to install WebPack.
npm install --save-dev webpack webpack-cli webpack-dev-server
Here,
We install loaders for WebPack as it 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.
npm i --save-dev style-loader css-loader babel-loader
Now let us create a WebPack configuration script with the following:
const path = require("path");
module.exports = {
// Entry point that indicates where
// should the webpack starts bundling
entry: "./index.jsx",
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",
},
};
And let’s see what all of this is doing:
entry: The entry point for our application. In React, this is the file where we use our renderer.mode: The mode in which the application is being run (development or production).output: Informing WebPack where to put our bundled code and the name of the file.module: Informing WebPack how and when to use the loaders we installed. We’re using regex to tell each loader which file extensions to target.Within the script in package.json add the following commands to run and build the project.
"scripts": {
"start":"npx webpack-dev-server --mode development --open --hot",
"build":"npx webpack --mode production",
}
Here,
<script> to bundle.js in dist folder for output.<!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>Not a Create React App</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>
npm start
And this runs the command on the terminal to run the project in development mode.
This is the hosted page.

This is the console.
