<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Rasveer Bansi</title>
    <description>The latest articles on Forem by Rasveer Bansi (@rasveerb).</description>
    <link>https://forem.com/rasveerb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F578708%2Faf5990f1-9a58-4165-8237-c4d2809f4919.jpeg</url>
      <title>Forem: Rasveer Bansi</title>
      <link>https://forem.com/rasveerb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rasveerb"/>
    <language>en</language>
    <item>
      <title>How to set up React projects without using create-react-app</title>
      <dc:creator>Rasveer Bansi</dc:creator>
      <pubDate>Thu, 25 Mar 2021 21:52:05 +0000</pubDate>
      <link>https://forem.com/rasveerb/how-to-set-up-react-projects-without-using-create-react-app-3hkm</link>
      <guid>https://forem.com/rasveerb/how-to-set-up-react-projects-without-using-create-react-app-3hkm</guid>
      <description>&lt;p&gt;This step-by-step guide explains how to set up a React application from scratch, without using any of the &lt;em&gt;"create-react-app"&lt;/em&gt; scripts.&lt;/p&gt;

&lt;h2&gt;
  
  
  DISCLAIMER: 
&lt;/h2&gt;

&lt;p&gt;I want to begin this guide by noting that there is absolutely nothing wrong with using the &lt;a href="https://reactjs.org/docs/create-a-new-react-app.html"&gt;&lt;em&gt;"create-react-app"&lt;/em&gt;&lt;/a&gt; scripts when writing your code. In the past I have found this to be a very helpful and time efficient way of setting up my React applications, especially when I was new to learning React. These scripts set up Webpack and Babel for you, so you can focus purely on developing the application itself. If you want to learn more about the set up process and how these tools relate to your React application, then I highly recommend following the steps presented in this guide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/bAplZhiLAsNnG/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/bAplZhiLAsNnG/giphy.gif" alt="Bruce Almighty Typing Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Setting up your project 
&lt;/h3&gt;

&lt;p&gt;First you will need to download and install either &lt;a href="https://nodejs.org/en/download/"&gt;Node.js&lt;/a&gt; or &lt;a href="https://classic.yarnpkg.com/en/docs/install/#windows-stable"&gt;Yarn&lt;/a&gt; to use on your machine.&lt;/p&gt;

&lt;p&gt;Once this is installed, you should create an empty directory for your project to live in. You can create and enter a new directory, by writing the following commands in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;gt; mkdir myProject              
 &amp;gt; cd myProject
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Initialising your project
&lt;/h3&gt;

&lt;p&gt;Now you have a directory, you will need to initialise your project and create a &lt;em&gt;package.json&lt;/em&gt; file. &lt;br&gt;
This file will: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;list all the packages that your project depends on&lt;/li&gt;
&lt;li&gt;specify which version of those packages can be used in your project (using &lt;a href="https://docs.npmjs.com/about-semantic-versioning"&gt;semantic versioning rules&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;make it easier to share your work with other developers as they can use this file to reproduce your build&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can initialise your project by running a single command in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;##for node use:
 &amp;gt; npm init

##for yarn use:
 &amp;gt; yarn init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will prompt a series of commands asking you to enter information about your project so a relevant &lt;em&gt;package.json&lt;/em&gt; file can be created. The information you enter will sit as a json object at the top of your &lt;em&gt;package.json&lt;/em&gt; file. (You can change this information at any point whilst developing your project, so it is okay if you want to leave any answers empty for now).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; package name: (myProject)
 version: (1.0.0)
 description: //enter a short description about your project 
 here
 entry point: (index.js)
 test command: //enter your test command here e.g test
 git repository: //enter the link for your github repo (if 
 you already cloned a github repo and created your directory 
 inside of it, the repo link will automatically appear here)
 keywords: //enter any optional keywords here
 author: Rasveer Bansi
 license: (ISC)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next you will want to create a src folder inside your myProject directory, where you can store all the source code for your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; mkdir src 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your project structure should now resemble:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myProject 
| - src 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Setting up Webpack
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://webpack.js.org/"&gt;Webpack&lt;/a&gt; is a tool which processes and bundles your application, making your code more usable for browsers. Your application contains modules of reusable code (e.g javascript, node_modules, images and CSS styles) which all come together to create your application. Webpack separates this code based on how it's used by your application, making it easier for the browser to follow.&lt;/p&gt;

&lt;p&gt;You can install Webpack by running the following command in your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;##for node use:
 &amp;gt; npm install --save-dev webpack webpack-dev-server webpack-cli

##for yarn use: 
 &amp;gt; yarn add webpack webpack-dev-server webpack-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next you will need to create a &lt;em&gt;webpack.config.js&lt;/em&gt; file in your &lt;em&gt;myProject&lt;/em&gt; directory. In this file you will need to save the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path');
  module.exports = {

//define entry file and output
    entry: './src/index.js',
    output: {
     path: path.resolve('dist'),
     filename: 'main.js'
    },
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;entry&lt;/strong&gt;&lt;/em&gt; - This is the top level set of files that you want to include in your build. This can be either a single file or an array of files.&lt;br&gt;
(&lt;em&gt;Nb&lt;/em&gt;: in the example above we have passed a single file &lt;em&gt;index.js&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;output&lt;/strong&gt;&lt;/em&gt; - This is an object containing your output configuration. In the example above, we specify the name of the file we want Webpack to build (&lt;em&gt;main.js&lt;/em&gt;) and the project path for where this file should be stored.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 4: Setting up Babel
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://babeljs.io/docs/en/"&gt;Babel&lt;/a&gt; is a javascript compiler that converts ES6 code into a backwards compatible version of itself, so older browsers can also run your application.&lt;/p&gt;

&lt;p&gt;To install Babel you need to run the following commands in your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;##for node use:
 &amp;gt; npm install --save-dev babel-core babel-loader 
 babel-preset-es2015 babel-preset-react @babel/core 
 @babel/preset-env @babel/preset-react

 &amp;gt; npm install --save-dev babel-plugin-transform-object-rest-spread 

##for yarn use:
 &amp;gt; yarn add  babel-core babel-loader babel-preset-es2015
 babel-preset-react @babel/core @babel/preset-env 
 @babel/preset-react

 &amp;gt; yarn add babel-plugin-transform-object-rest-spread
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next you will need to create a &lt;em&gt;babel.config.json&lt;/em&gt; file in your &lt;em&gt;myProject&lt;/em&gt; directory. In this file you will need to save the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "presets":[
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "env": {
    "test": {
      "plugins": ["@babel/plugin-transform-modules-commonjs"]
    }
  },
  "plugins": [ "transform-object-rest-spread" ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;presets&lt;/strong&gt;&lt;/em&gt; - This lets you use the react and es2015 presets you installed earlier to compile your code into something compatible for older browsers. By installing &lt;em&gt;"&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;
/preset-env"&lt;/em&gt; you have included &lt;em&gt;&lt;strong&gt;all&lt;/strong&gt;&lt;/em&gt; javascript versions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;plugins&lt;/strong&gt;&lt;/em&gt; - These are individual plugins you can install which compile specific ES6 modules into something more compatible for older browsers.&lt;/p&gt;

&lt;p&gt;Next you will need to add your Babel loader to your &lt;em&gt;webpack.config.js&lt;/em&gt; file. To do this you need to create a &lt;em&gt;module&lt;/em&gt; object and inside this object create an array called &lt;em&gt;rules&lt;/em&gt;. This array stores each of your loaders as individual objects which are defined by their key "&lt;em&gt;loader&lt;/em&gt;".&lt;/p&gt;

&lt;p&gt;The entire file should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const path = require('path');
 module.exports = {
 //define entry file and output
  entry: './src/index.js',
  output: {
    path: path.resolve('dist'),
    filename: 'main.js'
  },
 //define babel loader
  module: {
    rules: [
      { test: /\.jsx?$/, loader: 'babel-loader', 
        exclude: /node_modules/ 
      },
      { test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      }
    ]
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;test&lt;/strong&gt;&lt;/em&gt; - A regular expression (regex) that tests what kind of files to run through your loader. As you can see, this example has added regex to test all files with the ES6 extension of &lt;em&gt;jsx&lt;/em&gt; and has also created a separate object for &lt;em&gt;CSS&lt;/em&gt; files.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;loader&lt;/strong&gt;&lt;/em&gt; - The name of the loader you are going to use (eg &lt;em&gt;babel-loader or style-loader / css-loader&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;exclude&lt;/strong&gt;&lt;/em&gt; - Tells us which files the loader should exclude or ignore. This example has chosen to exclude and ignore the &lt;em&gt;node_modules&lt;/em&gt; folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Setting up your entry files
&lt;/h3&gt;

&lt;p&gt;Now you have set up your project configuration (&lt;em&gt;yay!&lt;/em&gt;), you will need to create some entry files so you can begin developing your application.&lt;/p&gt;

&lt;p&gt;You can begin by creating two entry files: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an &lt;em&gt;index.js&lt;/em&gt; file inside your &lt;em&gt;src&lt;/em&gt; folder&lt;/li&gt;
&lt;li&gt;an &lt;em&gt;index.html&lt;/em&gt; file inside your &lt;em&gt;myProject&lt;/em&gt; directory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your file structure should resemble this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; | - src
 |    | - index.js
 |
 | - index.html
 | - webpack.config.js
 | - babel.config.json
 | - package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;index.js&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
This is the same &lt;em&gt;index.js&lt;/em&gt; file that is configured as the entry point inside your &lt;em&gt;webpack.config.js&lt;/em&gt; file. For now it is okay to place the following code inside it, however once you begin developing your application you will need to update this file to contain the relevant starting component that will render your application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('hello world!'); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;index.html&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
This file provides structure for your application and is used by the web server as a &lt;em&gt;"default document"&lt;/em&gt;. It contains a script tag which directs your browser to the webpack file that was specifically built for the browser to follow.&lt;/p&gt;

&lt;p&gt;Place the following code into your &lt;em&gt;index.html&lt;/em&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
   &amp;lt;head&amp;gt;
     &amp;lt;meta charset="utf-8"&amp;gt;
     &amp;lt;title&amp;gt;React&amp;lt;/title&amp;gt;
   &amp;lt;/head&amp;gt;
   &amp;lt;body&amp;gt;
     &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
     &amp;lt;script src="main.js"&amp;gt;&amp;lt;/script&amp;gt;
   &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, &lt;em&gt;&lt;/em&gt; is the tag which directs our browser to the webpack file (&lt;em&gt;main.js&lt;/em&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6: Setting up your project scripts
&lt;/h3&gt;

&lt;p&gt;Now that you have some entry files set up, you will need to write some scripts that tell your application what to do. These will also be the commands that you use in your terminal when working on your application. For now you can just write two scripts to build and start your application.&lt;/p&gt;

&lt;p&gt;To do this you need to create a &lt;em&gt;scripts&lt;/em&gt; object in your &lt;em&gt;package.json&lt;/em&gt; file. Inside this object you will write each of your scripts as their own individual script object. For each script object you want to include, the key is the command keyword needed to run your script and the value is an instruction for your application to follow.&lt;/p&gt;

&lt;p&gt;Place the following &lt;em&gt;scripts&lt;/em&gt; object into your &lt;em&gt;package.json&lt;/em&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
   "start": "webpack serve --mode development --env 
 development",
   "build": "webpack"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you want to start your application using the terminal, you can use the script object which has the key &lt;em&gt;"start"&lt;/em&gt;. This will tell your application to start running the Webpack server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ##for Node use:
 &amp;gt; npm run start

 ##for Yarn use:
 &amp;gt; yarn start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 7: Setting up React
&lt;/h3&gt;

&lt;p&gt;The last thing you need to do is set up &lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt; so you can start developing a react application.&lt;/p&gt;

&lt;p&gt;To do this we need to install React in the &lt;em&gt;myProject&lt;/em&gt; directory by entering the following command into your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ##for Node use:
 &amp;gt; npm install --save react react-dom

 ##for Yarn use:
 &amp;gt; yarn add react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations you have now finished setting up and can begin developing your React application 🎉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/s2qXK8wAvkHTO/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/s2qXK8wAvkHTO/giphy.gif" alt="celebration gif"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webpack</category>
      <category>babel</category>
    </item>
  </channel>
</rss>
