{"id":48,"date":"2021-12-29T16:00:55","date_gmt":"2021-12-29T16:00:55","guid":{"rendered":"http:\/\/frontendguruji.com\/?p=48"},"modified":"2022-07-01T18:13:03","modified_gmt":"2022-07-01T18:13:03","slug":"how-to-setup-react-project-without-create-react-app","status":"publish","type":"post","link":"https:\/\/frontendguruji.com\/blog\/how-to-setup-react-project-without-create-react-app\/","title":{"rendered":"How to setup React project without create-react-app [ Best way to learn React environment] [react-router-dom v6]"},"content":{"rendered":"\n<p>Hello Friends! &#x1f64f;.   <strong>React JS Tutorial<\/strong> we will setup <strong>React project without create-react-app<\/strong> command&#x1f60e;, this is the best way to learn React as we <strong>will setup react environment<\/strong> and will explore <strong>react project dependencies<\/strong>. <\/p>\n\n\n\n<p>React JS project can be setup using only one <a rel=\"noreferrer noopener\" href=\"https:\/\/create-react-app.dev\/docs\/getting-started\" target=\"_blank\">command <\/a>&amp; it only takes few minutes&#x1f680; . Everyone use it &amp; while command does its magic,  all sit relaxed on their chair enjoing whatsapp\/Music&#8230; whatever&#8230; depends on your network speed&#x1f923; . But in this tutorial, we will setup react project from scratch.<\/p>\n\n\n\n<p><strong>Chalo suru kiya jaye!<\/strong>&#x1f37b;<strong>  Lets start! <\/strong>&#x1f57a;<\/p>\n\n\n\n<h2>Step 1: Install\/Upgrade\/Verify Node<\/h2>\n\n\n\n<p>To start with setup React project, first please verify if your machine has node v &gt;= 14 &amp; npm v &gt;=5.6.<br>If not, then <a rel=\"noreferrer noopener\" href=\"https:\/\/nodejs.org\/en\/\" target=\"_blank\">install node package here<\/a>.<\/p>\n\n\n\n<p>While upgrading node package on my machine to latest version: 16.13.1 which includes npm 8.1.2, I faced one issue, may be you also get this type of error, check here to resolve it: <a rel=\"noreferrer noopener\" href=\"http:\/\/frontendguruji.com\/warn-npm-does-not-support-node-js-v16-13-1\/\" target=\"_blank\">WARN npm does not support Node.js v16.13.1<\/a><\/p>\n\n\n\n<h2>Step 2: Initialize a folder<\/h2>\n\n\n\n<p>So friends! first thing we have to do is to create a blank folder &#x1f4c1; &amp; initialize it using below command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm init<\/code><\/pre>\n\n\n\n<p>This command will run a wizard of questions\/choices related to the project &amp; we have to answer\/select them. This basically creates a <strong>package.json<\/strong> file which will include all our answers\/selections.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"659\" height=\"695\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2021\/12\/image-5.png\" alt=\"React JS,setup react project,react tutorial,configure webpack,configure babel,setup react project without create-react-app\" class=\"wp-image-91\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2021\/12\/image-5.png 659w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2021\/12\/image-5-284x300.png 284w\" sizes=\"(max-width: 659px) 100vw, 659px\" \/><\/figure>\n\n\n\n<h2>Step 3: Adding React Project Dependencies<\/h2>\n\n\n\n<h3>Adding React<\/h3>\n\n\n\n<p>To use React we need core <strong>react<\/strong> package that contains the functionality needed to define React components.<br>Then we need renderers like <strong>react-dom<\/strong> to render the app on the web browser, or<strong> react-native<\/strong> to render the app on iOS\/Android environment. <br>In our case we need react-dom as we are building for the web browser.<br>And lastly <strong>react-router-dom<\/strong> to handle routing in the application , it contains DOM bindings for React Router. <\/p>\n\n\n\n<p>As we have installed the latest  <strong>react-router-dom<\/strong>  package i.e. v6, there are many breaking changes. I got to know about these breaking changes when I got stuck in a issue: <a rel=\"noreferrer noopener\" href=\"http:\/\/frontendguruji.com\/warn-export-switch-imported-as-switch-was-not-found-in-react-router-dom\/\" target=\"_blank\">WARN export \u2018Switch\u2019 (imported as \u2018Switch\u2019) was not found in \u2018react-router-dom\u2019<\/a>. So better read the v6 documentation before diving into it &#x1f60e;. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install react react-dom react-router-dom<\/code><\/pre>\n\n\n\n<h3>Adding Babel &#8211; JS Compiler<\/h3>\n\n\n\n<p>Babel is a compiler which converts ES6 code to ES5 code, so that older browser can also run the application.<br>So basically It lets us write modern JavaScript code that still works in older browsers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save-dev @babel\/core @babel\/cli<\/code><\/pre>\n\n\n\n<p><strong>babel\/core<\/strong> contains the main babel functionality i.e. converting the modern JS to ES5.<br><strong>babel\/cli <\/strong>is used to run babel from command line using its inbuild commands<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save-dev @babel\/preset-env @babel\/preset-react<\/code><\/pre>\n\n\n\n<p>These presets tells babel to target specific type of code, like in our case we want to convert ES6+ and JSX code to ES5 code which is needed for React project. We will use these presets while doing configurations later in this tutorial.<\/p>\n\n\n\n<h3>Adding Webpack &#8211; Bundler<\/h3>\n\n\n\n<p>Webpack is a bundler for modern JS projects, this basically combines all the modules used in the project to one or multiple bundles<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save-dev webpack webpack-cli webpack-dev-server<\/code><\/pre>\n\n\n\n<p><strong>webpack <\/strong>contains the core functionality and<strong> webpack-cli<\/strong> is to run webpack from command line.<br><strong>webpack-dev-server<\/strong> is to view projet in browser locally while development.<\/p>\n\n\n\n<p>By default Webpack process only JS files and to process other assest like images, CSS &amp; HTML it requires loaders, below are some basic  loaders:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save-dev babel-loader html-loader style-loader css-loader\n<\/code><\/pre>\n\n\n\n<p><strong>babel-loader<\/strong>: loader for babel files<br><strong>html-loader<\/strong>: loader for HTML files<br><strong>css-loader<\/strong>: loader for CSS files<br><strong>style-loader<\/strong>: loader to inject styles into DOM<\/p>\n\n\n\n<p><em>Optional<\/em>: To use CSS preprocessors like SCSS, you will have to install <strong>sass-loader<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save-dev sass-loader sass<\/code><\/pre>\n\n\n\n<p>By default Webpack bundles CSS into JS, to generate seperate CSS file, you will have to install <strong>mini-css-extract-plugin<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save-dev mini-css-extract-plugin\n<\/code><\/pre>\n\n\n\n<p>Now we need a HTML page to inject all the files\/bundles generated by Webpack. For that we need to add <strong>html-webpack-plugin<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install --save-dev html-webpack-plugin<\/code><\/pre>\n\n\n\n<p>Now our <strong>package.json<\/strong> file looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"709\" height=\"692\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2021\/12\/image-6.png\" alt=\"React JS,setup react project,react tutorial,configure webpack,configure babel,setup react project without create-react-app\" class=\"wp-image-101\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2021\/12\/image-6.png 709w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2021\/12\/image-6-300x293.png 300w\" sizes=\"(max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p>That&#8217;s all in the dependencies &#x1f605;, lets jump to create an App then some configurations and then will try to run it &#x1f57a;.<\/p>\n\n\n\n<h2> Step 4: Creating &#8216;Hello World&#8217; React App<\/h2>\n\n\n\n<p>Create <strong>src <\/strong>folder&#x1f4c1; and then create a component <strong>App.js<\/strong> in it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\/\/ src\/App.js<\/strong>\nimport React from \"react\";\n\nconst App = () =&gt; {\n   return &lt;div className=\"message\"&gt;Hello, World!&lt;\/div&gt;;\n};\n\nexport default App;<\/code><\/pre>\n\n\n\n<p>Now create a style for the App component <strong>styles.scss<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ <strong>src\/styles.scss<\/strong>\n.message {\n    text-align: center;\n    font-size: 30px;\n}<\/code><\/pre>\n\n\n\n<p>Now we need to create the main entry file (<strong>index.js<\/strong>) which will render the app in a DOM element having id=&#8221;<strong>root<\/strong>&#8220;, this element we will create later in this tutorial. This rendering is done by <strong>react-dom<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>\/\/ src\/index.js<\/strong>\nimport React from \"react\";\nimport ReactDOM from \"react-dom\";\nimport { BrowserRouter, Route, Routes } from \"react-router-dom\";\n\nimport App from \".\/App.js\";\nimport \".\/styles.scss\";\n\nconst routing = (\n  &lt;BrowserRouter&gt;\n    &lt;Routes&gt;\n      &lt;Route path=\"\/\" element={&lt;App \/&gt;} \/&gt;\n    &lt;\/Routes&gt;\n  &lt;\/BrowserRouter&gt;\n);\n\nReactDOM.<strong>render<\/strong>(routing, document.getElementById(\"<strong>root<\/strong>\"));<\/code><\/pre>\n\n\n\n<h2>Step 5: Configure Webpack<\/h2>\n\n\n\n<p>Now we will have to configure webpack, webpack-dev-server, loaders and plugins that we installed above &#x1f605;, lets start!<\/p>\n\n\n\n<p>Create a blank webpack configuration file <strong>webpack.config.js<\/strong>  in the root folder and start adding the below configurations.<\/p>\n\n\n\n<p>Below is the main <strong>configuration for webpack<\/strong>, here you have to give the <strong>entry<\/strong> point of the file (<em>index.js<\/em> in our case) and <strong>output <\/strong>path for the generated bundles (<em>dist <\/em>in our case). Empty configurations like devServer, rules, plugins, optimization will configure one by one below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \/<strong>webpack.config.js<\/strong>\n\/\/ imports\nconst path = require('path');\n\n\/\/ module\nmodule.exports = {\n  mode: 'development',\n  <strong>entry<\/strong>: '.\/src\/<strong>index.js<\/strong>',\n  <strong>output<\/strong>: {\n    path: path.resolve(__dirname, '<strong>dist<\/strong>')\n  },\n  devServer: {},\n  rules: &#91;],\n  plugins: &#91;],\n  optimization: {}\n};<\/code><\/pre>\n\n\n\n<p>Now we will configure <strong>loaders<\/strong> in module <strong>rules: [] <\/strong>defined above<\/p>\n\n\n\n<h3>Configure <strong>SCSS<\/strong>.<\/h3>\n\n\n\n<p><em>Note: Loaders are evaluated\/executed from bottom to top.<\/em><br>Here execution starts with sass-loader, continues with css-loader and finally ends with style-loader.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  test:\/\\.scss$\/,\n  use: &#91;{\n      loader: 'style-loader'\n    },\n    {\n      loader: 'css-loader'\n    },\n    {\n      loader: 'sass-loader'\n    }\n  ]\n}<\/code><\/pre>\n\n\n\n<h3>Configure <strong>Babel<\/strong><\/h3>\n\n\n\n<p><strong>babel-loader<\/strong> configuration. <br><strong>options.preset<\/strong> will tell babel to use these presets while compiling the code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  test: \/\\.(js|jsx)$\/,\n  exclude: \/(node_modules)\/,\n  use: {\n    loader: 'babel-loader',\n    options: {\n      presets: &#91;\"@babel\/preset-env\", \"@babel\/preset-react\"],\n    },\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>html-loader<\/strong> configuration<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  test: \/\\.html$\/i,\n  loader: \"html-loader\",\n}<\/code><\/pre>\n\n\n\n<h3>Configure <strong>plugins<\/strong><\/h3>\n\n\n\n<p><strong>mini-css-extract-plugin<\/strong> will extract the styles from bundle and generates a seperate file styles.css in the dist folder.<br><strong>html-webpack-plugin<\/strong> will create html file at <strong>dist\/index.html<\/strong> and will automatically inject all the webpack generated files along with the CSS gerenated by mini-css-extract-plugin.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ <strong>add in imports<\/strong>\nconst MiniCssExtractPlugin = require(\"mini-css-extract-plugin\");\nconst HtmlWebpackPlugin = require('html-webpack-plugin');\n\n\/\/ <strong>add in plugins array<\/strong>\nnew MiniCssExtractPlugin(),\nnew HtmlWebpackPlugin({\n  template:\"<strong>.\/src\/index.html<\/strong>\"\n})\n\n\/\/ <strong>modify the CSS loader configuration<\/strong>\n{\n  test: \/\\.scss$\/,\n  use: &#91;{\n      loader: \"style-loader\"\n    },\n    <strong>{\n      loader: MiniCssExtractPlugin.loader,\n      options: {\n        esModule: false,\n      },\n    },<\/strong>\n    {\n      loader: \"css-loader\"\n    },\n    {\n      loader: \"sass-loader\"\n    },\n  ],\n}\n\n<strong>\/\/ update optimization property<\/strong>\noptimization: {\n  splitChunks: {\n    cacheGroups: {\n      styles: {\n        name: \"styles\",\n        type: \"css\/mini-extract\",\n        chunks: \"all\",\n        enforce: true,\n      },\n    },\n  },\n}\n<\/code><\/pre>\n\n\n\n<p><em>Use of  <strong>esModule: false<\/strong> in above configuration<\/em>: while compiling the App I was getting some warnings related to <strong>mini-css-extract-plugin<\/strong> you can check that here: <a rel=\"noreferrer noopener\" href=\"http:\/\/frontendguruji.com\/warn-export-default-imported-as-content-was-not-found\/\" target=\"_blank\">WARN export \u2018default\u2019 (imported as \u2018content\u2019) was not found <\/a>.<\/p>\n\n\n\n<p>For  <strong>html-webpack-plugin<\/strong> we need to create a html template which will be used by this plugin to inject the scripts &amp; styles generated by webpack. For tha create <strong>index.html<\/strong> file inside src folder &#x1f4c1;. This html will contain the element having id=&#8221;<strong>root<\/strong>&#8220;, this will be the element where app will load using react-dom.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n  &lt;head&gt;\n    &lt;meta charset=\"UTF-8\" \/&gt;\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" \/&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/&gt;\n    &lt;title&gt;Hello World App&lt;\/title&gt;\n  &lt;\/head&gt;\n  &lt;body&gt;\n    &lt;div id=\"<strong>root<\/strong>\"&gt;&lt;\/div&gt;\n  &lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<p>Now configure <strong>webpack-dev-server<\/strong>. It needs <em>directory <\/em>which must be path to the <strong>dist <\/strong>folder and a <em>port <\/em>to run on. If you want webpack to open the App in browser automatically provide <strong>open: true<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>devServer: {\n  static: {\n    <strong>directory<\/strong>: path.join(__dirname, 'dist'),\n  },\n  <strong>port<\/strong>: 9000,\n  open: true\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/github.com\/mandeeppasbola\/setup-react-without-create-react-app\/blob\/main\/webpack.config.js\" target=\"_blank\" rel=\"noreferrer noopener\">Check here the final <strong>webpack.config.js<\/strong> file<\/a>.<\/p>\n\n\n\n<h2>Step 6: Adding build commands<\/h2>\n\n\n\n<p>To build or run the project we will have to add some commands in package.json file under scripts property.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"scripts\": {\n  \"start\": \"webpack-dev-server\",\n  \"build\": \"webpack\"\n}<\/code><\/pre>\n\n\n\n<p><strong>start <\/strong>command will execute webpack-dev-server which will build the project <em>in memory<\/em> and load the page on the browser. <br><strong>build<\/strong> command will build the project in <em>dist <\/em>folder with all the files.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/mandeeppasbola\/setup-react-without-create-react-app\/blob\/main\/package-lock.json\" target=\"_blank\" rel=\"noreferrer noopener\">Check here the final <strong>package.json<\/strong> file<\/a>.<\/p>\n\n\n\n<p>Now the final moment&#x1f605; &#x1f973;&#x1f973; <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm run start \/\/ to start the app in browser\nnpm run build \/\/ to generate the dist folder with files<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"604\" height=\"170\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2021\/12\/image-14.png\" alt=\"React JS,setup react project,react tutorial,configure webpack,configure babel,setup react project without create-react-app\" class=\"wp-image-123\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2021\/12\/image-14.png 604w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2021\/12\/image-14-300x84.png 300w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/figure>\n\n\n\n<p>And in source view, you can see the injected JS and Style file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img width=\"714\" height=\"285\" src=\"http:\/\/frontendguruji.com\/wp-content\/uploads\/2021\/12\/image-20.png\" alt=\"React JS,setup react project,react tutorial,configure webpack,configure babel,setup react project without create-react-app\" class=\"wp-image-150\" srcset=\"https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2021\/12\/image-20.png 714w, https:\/\/frontendguruji.com\/blog\/wp-content\/uploads\/2021\/12\/image-20-300x120.png 300w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/figure>\n\n\n\n<p>&#x1f449;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/mandeeppasbola\/setup-react-without-create-react-app\" target=\"_blank\">Checkout the project here.<\/a> <\/p>\n\n\n\n<p>Thats all in this <strong>React JS tutorial<\/strong>, here we learned how to <strong>setup react project without create-react-app<\/strong> and also covered how to <strong>configure webpack<\/strong>, <strong>configure babel<\/strong>, <strong>configure Webpack loaders and plugins<\/strong>, <strong>added build commands<\/strong>. <\/p>\n\n\n\n<p>&#x2753;If you have any question or suggestion, please feel free to comment.<\/p>\n\n\n\n<p>&#x1f449; <a href=\"http:\/\/frontendguruji.com\/how-to-create-react-app-in-docker-container\/\">Do you know we can setup React App inside a docker container, check here!<\/a><\/p>\n\n\n\n<p>Thanks&#x1f64f;. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this React JS tutorial, you will learn how to setup react project without create-react-app and also how to configure webpack, configure babel, configure Webpack loaders and plugins and add build commands.<\/p>\n","protected":false},"author":1,"featured_media":186,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[4],"tags":[29,21,18,7,43,35,13,38,37,23,36],"_links":{"self":[{"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/posts\/48"}],"collection":[{"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":0,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/media\/186"}],"wp:attachment":[{"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendguruji.com\/blog\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}