<?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>DEV Community: Bhavuk kalra</title>
    <description>The latest articles on DEV Community by Bhavuk kalra (@bhavukkalra).</description>
    <link>https://dev.to/bhavukkalra</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%2F1125452%2F392fbf98-3a4f-40c2-9699-ce44da133f68.png</url>
      <title>DEV Community: Bhavuk kalra</title>
      <link>https://dev.to/bhavukkalra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhavukkalra"/>
    <language>en</language>
    <item>
      <title>Creating my own programming language from scratch - Day 1</title>
      <dc:creator>Bhavuk kalra</dc:creator>
      <pubDate>Tue, 22 Apr 2025 13:13:13 +0000</pubDate>
      <link>https://dev.to/bhavukkalra/creating-my-own-programming-language-from-scratch-day-1-260c</link>
      <guid>https://dev.to/bhavukkalra/creating-my-own-programming-language-from-scratch-day-1-260c</guid>
      <description>&lt;h2&gt;
  
  
  Why create this series?
&lt;/h2&gt;

&lt;p&gt;I guess whenever we launch series we should ask ourselves why that series was created in the first place and It could be any of the following things&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There was a gap in the market, That you wanted to fix.&lt;/li&gt;
&lt;li&gt;You wanted to do it because its your job of writing content&lt;/li&gt;
&lt;li&gt;You see it as a notemaking exercise that you can look back later and revise.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For me its none of the above. I wanna expand this to a fully unfiltered guide on what my state of  mind looks like and how it evolves after I have created my first language. &lt;/p&gt;

&lt;p&gt;Also, Let's admit it has been always in the back of our mind to create our own programming language someday right, Ever since we started coding?&lt;/p&gt;

&lt;h2&gt;
  
  
  Initial Exploration
&lt;/h2&gt;

&lt;p&gt;So we start with a simple youtube search first of all and we landed with a pretty good video(Its in hindi tho)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=rTuTLc_u6qw" rel="noopener noreferrer"&gt;I made my own programming language&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From here we got to understand the basics of what a components a programming language requires to function (and a refresher of my college course)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lexer/Tokenizer (Converts the program into tokens)&lt;/li&gt;
&lt;li&gt;Abstract Syntax Tree creator (Takes input of the tokens in the previous step and Creates a AST which holds the logical flow how the program should process the tokens step by step)&lt;/li&gt;
&lt;li&gt;Code Generator (Takes the input of created AST and converts it into machine understandable language)&lt;/li&gt;
&lt;li&gt;Code Runner (Runs the machine code generated in the previous step)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I am pretty sure of how I used the above layman terms to describe each component that is required by a programming language to run. They are all going to get their technical descriptions later on and maybe new components might get added as well. &lt;/p&gt;

&lt;h2&gt;
  
  
  Self study material
&lt;/h2&gt;

&lt;p&gt;While  looking for more material to study through on the internet I came across Robert Nystrom &lt;a href="https://craftinginterpreters.com/" rel="noopener noreferrer"&gt;CRAFTING INTERPRETERS&lt;/a&gt;. So called the &lt;code&gt;HOLY GRAIL OF HOW TO MAKE YOUR OWN PROGRAMMING LANGUAGE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Exactly what we needed! &lt;/p&gt;

&lt;p&gt;Now we start reading this&lt;/p&gt;

&lt;p&gt;Here is a summary of few pages down the line&lt;/p&gt;

&lt;h2&gt;
  
  
  Started Reading the Book
&lt;/h2&gt;

&lt;p&gt;Here is what I got to know so far&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The book is written to use Java, but there are open source implementations of the same java snippets provided by the community (I Love open source man 💕). We are going to use the &lt;code&gt;Javascript&lt;/code&gt; and &lt;code&gt;C++&lt;/code&gt; implementations of the same&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The author suggests to be versed with recursion, dynamic arrays, trees, graphs, and hash tables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now connecting the dots backwards, It seems like we don't need to implement the lexer from scratch these days for Javascript as there are already libraries for it such as  &lt;a href="https://en.wikipedia.org/wiki/Lex_(software)" rel="noopener noreferrer"&gt;Lex&lt;/a&gt; and &lt;a href="https://en.wikipedia.org/wiki/Yacc" rel="noopener noreferrer"&gt;Yacc&lt;/a&gt;, so-called &lt;strong&gt;compiler-compilers&lt;/strong&gt;. &lt;br&gt;
That automatically generate some of the source files for an implementation from some higher-level description. &lt;br&gt;
(We'll explore on what that means exactly later I guess)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is what will be covered in the book. We'll make Two interpreters&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First Interpreter - Based on Java. Focuses on being correct to get the basics down. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second Interpreter - Based on C. Builds upon on the previous implementation and focuses in being fast&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The C interpreter will contain a compiler that translates Lox to an efficient bytecode representation, which it then executes. This is the same technique used by implementations of Lua, Python, Ruby, PHP, and many other successful languages. We’ll even try our hand at benchmarking and optimization&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Everything we did in JAVA and took it for granted will be implemented from scratch (I am personally very excited about this). Such as &lt;code&gt;Dynamic Arrays&lt;/code&gt;, &lt;code&gt;Hash Tables&lt;/code&gt; and We'll also create our own &lt;code&gt;Garbage Collector&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;At the end we are given this assignment. &lt;/p&gt;

&lt;h3&gt;
  
  
  Assignment
&lt;/h3&gt;

&lt;p&gt;Define a doubly linked list of heap-allocated strings. Write functions to insert, find, and delete items from it to get a practice of pointers and Test them.&lt;/p&gt;

&lt;p&gt;So first off what are &lt;strong&gt;&lt;code&gt;HEAP ALLOCATED STRINGS&lt;/code&gt;&lt;/strong&gt;. A quick Chat GPT search gives us this -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above is stack allocated string in C&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STACK STRINGS&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It's stored on the stack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It has a fixed size and goes away when the function returns.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;heapStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;strcpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heapStr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Always free heap strings&lt;/span&gt;
&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heapStr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// OR&lt;/span&gt;

&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;heapStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strdup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Always free heap strings&lt;/span&gt;
&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;heapStr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;strdup&lt;/code&gt; allocates memory and copies the string.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;heapStr&lt;/code&gt; must be &lt;code&gt;free()&lt;/code&gt;d  when done to avoid memory leaks.&lt;/p&gt;

&lt;p&gt;Now that we understood this. We can start implementing some of the components from day 2&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary of Day 1
&lt;/h2&gt;

&lt;p&gt;We started analyzing on what it takes to build a programming language from scratch. In that search we found that there are a few essential elements that needs to be in place before we starting working towards implementing interpreter such as Lexer, AST. I don't know about this stuff yet but I am sure we'll be just fine as we go along. For the time I'll be following the book by &lt;strong&gt;Robert Nystrom's&lt;/strong&gt; &lt;a href="https://craftinginterpreters.com/" rel="noopener noreferrer"&gt;CRAFTING INTERPRETERS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There he uses &lt;code&gt;Java&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt; but I am gonna try to convert that to &lt;code&gt;javascript&lt;/code&gt;. As in the end I wanna make a language which can be ran on a browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  TODO Day 2
&lt;/h2&gt;

&lt;p&gt;We are gonna implement the doubly linked list with basic functionalities such as insert, Delete, Find and Create i.e basic CRUD.&lt;/p&gt;

&lt;p&gt;Also start with the actual implementation of the Interpreter.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding Singleton Design Pattern with a Logger Example - Part 2</title>
      <dc:creator>Bhavuk kalra</dc:creator>
      <pubDate>Wed, 16 Apr 2025 22:17:01 +0000</pubDate>
      <link>https://dev.to/bhavukkalra/python-singleton-logger-part-2-thread-safety-race-conditions-lock-optimization-1l2o</link>
      <guid>https://dev.to/bhavukkalra/python-singleton-logger-part-2-thread-safety-race-conditions-lock-optimization-1l2o</guid>
      <description>&lt;p&gt;Continuing from where we left off: We successfully created a singleton class for &lt;code&gt;Logger&lt;/code&gt;, exposing a static method &lt;code&gt;getLogger&lt;/code&gt; for all users interacting with the class.&lt;/p&gt;

&lt;p&gt;To quickly recap, our &lt;code&gt;Logger&lt;/code&gt; class includes the following key features:&lt;/p&gt;

&lt;p&gt;🔹 Direct instantiation of the &lt;code&gt;Logger&lt;/code&gt; class is not allowed.&lt;/p&gt;

&lt;p&gt;🔹 Custom error handling is implemented to prevent direct instantiation.&lt;/p&gt;

&lt;p&gt;🔹 All users of the class use a common static &lt;code&gt;getLogger&lt;/code&gt; method to obtain an instance.&lt;/p&gt;

&lt;p&gt;However, the current implementation is not suitable for production usage, as it does not account for scenarios involving multithreading—i.e., it is not thread-safe. That’s exactly what we’ll address in this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-Requisites
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;threading&lt;/code&gt; vs &lt;code&gt;multiprocessing&lt;/code&gt; in python
&lt;/h3&gt;

&lt;h2&gt;
  
  
  🧵 Threading in Python
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is it?
&lt;/h3&gt;

&lt;p&gt;Threading lets you run multiple threads (tiny workers) inside a single process 🧠. They all share the same memory, like roommates sharing a house 🏠.&lt;/p&gt;

&lt;h3&gt;
  
  
  💡 How it works:
&lt;/h3&gt;

&lt;p&gt;🔹 All threads share the same memory space 🧠&lt;/p&gt;

&lt;p&gt;🔹 Great for I/O-bound tasks 📡 (e.g., web requests, file reading/writing).&lt;/p&gt;

&lt;p&gt;🔹 Controlled by the threading module 🧵&lt;/p&gt;

&lt;h3&gt;
  
  
  🧠 Key Concepts:
&lt;/h3&gt;

&lt;p&gt;🔹 Threads = lightweight 🚴‍♂️&lt;/p&gt;

&lt;p&gt;🔹 Memory is shared across threads 🏠&lt;/p&gt;

&lt;p&gt;🔹 But… there’s a pesky thing called the GIL (Global Interpreter Lock) ⛔ that means only one thread runs Python code at a time, so no true parallelism 😢 (for CPU-heavy stuff).&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ Pros:
&lt;/h3&gt;

&lt;p&gt;🔹 Super fast to start ⚡&lt;/p&gt;

&lt;p&gt;🔹 Low memory usage 🪶&lt;/p&gt;

&lt;p&gt;🔹 Threads can easily share data 🧠&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ Cons:
&lt;/h3&gt;

&lt;p&gt;🔹 Can’t utilize multiple CPU cores because of the GIL 🚫🧠&lt;/p&gt;

&lt;p&gt;🔹 Risk of bugs like race conditions and deadlocks 🕳️&lt;/p&gt;

&lt;h2&gt;
  
  
  🔥 Multiprocessing in Python
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is it?
&lt;/h3&gt;

&lt;p&gt;Multiprocessing creates separate processes, each with its own memory, and they run truly in parallel 🚀 — like different computers working together 🤝&lt;/p&gt;

&lt;h3&gt;
  
  
  💡 How it works:
&lt;/h3&gt;

&lt;p&gt;🔹 Uses the multiprocessing module 🛠️.&lt;/p&gt;

&lt;p&gt;🔹 Every process has its own memory space 📦.&lt;/p&gt;

&lt;p&gt;🔹 No GIL here — each process gets a core! 🧠🧠🧠&lt;/p&gt;

&lt;h3&gt;
  
  
  🧠 Best for:
&lt;/h3&gt;

&lt;p&gt;🔹 CPU-bound tasks 🧮 – number crunching, data processing, machine learning, etc.&lt;/p&gt;

&lt;p&gt;🔹 When you need real parallelism ⚙️⚙️⚙️&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ Pros
&lt;/h3&gt;

&lt;p&gt;🔹True parallelism 💥&lt;/p&gt;

&lt;p&gt;🔹Perfect for CPU-heavy tasks 🧠💪&lt;/p&gt;

&lt;p&gt;🔹No GIL = no problem 🚫🔒&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ Cons
&lt;/h3&gt;

&lt;p&gt;🔹 More memory usage 🧠💸&lt;/p&gt;

&lt;p&gt;🔹 Slower to spin up ⚙️&lt;/p&gt;

&lt;p&gt;🔹 Sharing data between processes is trickier 🧵➡️📦&lt;/p&gt;

&lt;h2&gt;
  
  
  Which one we would be using?
&lt;/h2&gt;

&lt;p&gt;Looking at different options that we have available for us in python ofcourse we are gonna go with &lt;code&gt;threading&lt;/code&gt; module as this a I/O usecase and we are not doing any heavy computation as well here. &lt;/p&gt;

&lt;h3&gt;
  
  
  Why is it not thread safe?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa4u82gigqf722o4v8ppn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa4u82gigqf722o4v8ppn.png" alt="Race condition example" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Referring to the diagram above. Imagine a scenario where there are two threads &lt;code&gt;Thread 1&lt;/code&gt; and &lt;code&gt;Thread 2&lt;/code&gt; both trying to access the &lt;code&gt;getLogger&lt;/code&gt; in the intial phases of running an application i.e &lt;code&gt;__loggerInstance&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🔹 Thread 1 checks if cls.__loggerInstance is None — it is, so it proceeds.&lt;/p&gt;

&lt;p&gt;🔹 Thread 2 runs at the exact same time, checks cls.__loggerInstance — still None, so it proceeds too.&lt;/p&gt;

&lt;p&gt;Both threads call &lt;code&gt;__new__()&lt;/code&gt; and &lt;code&gt;__init__()&lt;/code&gt; → 🧨 Boom! Two instances created!&lt;/p&gt;

&lt;p&gt;That’s called a &lt;code&gt;race condition&lt;/code&gt;, and it can definitely happen in multithreaded environments.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧪 How to Reproduce It
&lt;/h2&gt;

&lt;p&gt;You could artificially slow down the instantiation to provoke the issue: For Re-Producing the issue we'll make these changes to our already existing codebase&lt;/p&gt;

&lt;h3&gt;
  
  
  📄 Logger.py
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;# private Static variable to track num of instances created
&lt;/span&gt;    &lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="c1"&gt;# private static variable to denote if instance was created
&lt;/span&gt;    &lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Private constructor using __new__ to prevent direct instantiation
&lt;/span&gt;        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Use getLlogger() to create an instance.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Logger Instantiated, Total number of instances - &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nd"&gt;@classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Returns the singleton instance, creates one if it doesn't exist
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

            &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Simulate delay
&lt;/span&gt;
            &lt;span class="c1"&gt;# Bypass __new__ and directly instantiate the class
&lt;/span&gt;            &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

             &lt;span class="c1"&gt;# Trigger __init__ manually on first creation
&lt;/span&gt;            &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  📄 main.py
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;user1&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;doProcessingUser1&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;user2&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;doProcessingUser2&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;multiprocessing&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;doProcessingUser1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;doProcessingUser2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;All threads finished&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Added &lt;code&gt;time.sleep(0.1)&lt;/code&gt; to simulate delay while executing the &lt;code&gt;getLogger&lt;/code&gt; function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output (and we got a race condition)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Logger Instantiated, Total number of instances -  1
Log from the second user
Logger Instantiated, Total number of instances -  2
Log from the first user
All threads finished
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  ✅ How to Make It Thread-Safe
&lt;/h3&gt;

&lt;p&gt;Use a &lt;code&gt;threading.Lock&lt;/code&gt; to synchronize access to the singleton logic. Let's make these changes then we'll discuss on what we have edited so far.&lt;/p&gt;

&lt;h3&gt;
  
  
  📄 logger.py
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;# private Static variable to track num of instances created
&lt;/span&gt;    &lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="c1"&gt;# private static variable to denote if instance was created
&lt;/span&gt;    &lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="n"&gt;__mutexLock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;threading&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# For thread-safe singleton creation (private static)
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Private constructor using __new__ to prevent direct instantiation
&lt;/span&gt;        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Use getLlogger() to create an instance.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Logger Instantiated, Total number of instances - &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nd"&gt;@classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Returns the singleton instance, creates one if it doesn't exist
&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__mutexLock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

                &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Simulate delay
&lt;/span&gt;
                &lt;span class="c1"&gt;# Bypass __new__ and directly instantiate the class
&lt;/span&gt;                &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

                &lt;span class="c1"&gt;# Trigger __init__ manually on first creation
&lt;/span&gt;                &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstanc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have added these things&lt;/p&gt;

&lt;p&gt;🔹 private static variable &lt;code&gt;__mutexLock&lt;/code&gt; for letting only one thread i.e &lt;code&gt;Thread 1&lt;/code&gt; or &lt;code&gt;Thread 2&lt;/code&gt; to access the &lt;code&gt;getLogger&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;🔹 &lt;code&gt;with cls.__mutexLock:&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;What this does:&lt;/strong&gt;&lt;br&gt;
The with statement automatically acquires the lock when the block starts ✅&lt;/p&gt;

&lt;p&gt;And then it automatically releases the lock once the block is exited (even if an exception happens!) 🔓&lt;/p&gt;

&lt;p&gt;So technically, the lock is being released as soon as the indented block under with is done.&lt;/p&gt;

&lt;p&gt;✅ So no need to manually call .acquire() or .release() — the with block takes care of it cleanly and safely 🙌&lt;/p&gt;

&lt;p&gt;Here is a diagramatical representation of how this helps us in avoiding the original &lt;code&gt;race condition&lt;/code&gt; that we faced earlier&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrm40hl5qlcnfjk16o11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrm40hl5qlcnfjk16o11.png" alt="Race condition avoiding and colution" width="800" height="1476"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  🧵 Thread 1 and Thread 2 Execution Flow
&lt;/h2&gt;

&lt;p&gt;🟩 &lt;strong&gt;Thread 1 (Left side of the diagram):&lt;/strong&gt;&lt;br&gt;
Calls &lt;code&gt;getLogger()&lt;/code&gt;&lt;br&gt;
→ Thread 1 wants the logger instance.&lt;/p&gt;

&lt;p&gt;Acquires the lock 🔐&lt;br&gt;
→ Since it's the first thread in, it grabs the lock on __mutexLock.&lt;/p&gt;

&lt;p&gt;Executes &lt;code&gt;getLogger()&lt;/code&gt;&lt;br&gt;
→ Sees &lt;code&gt;__loggerInstance&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt;, creates the singleton logger instance.&lt;/p&gt;

&lt;p&gt;Releases the lock 🔓&lt;br&gt;
→ Done with critical section, allows other threads to enter.&lt;/p&gt;

&lt;p&gt;🟦&lt;strong&gt;Thread 2 (Right side of the diagram):&lt;/strong&gt;&lt;br&gt;
Also calls getLogger() at (roughly) the same time&lt;/p&gt;

&lt;p&gt;Waits for the lock ⏳&lt;br&gt;
→ It hits the lock, but Thread 1 already has it.&lt;/p&gt;

&lt;p&gt;Still waiting... 😬&lt;br&gt;
→ Thread 1 is doing its thing. Thread 2 just chills here.&lt;/p&gt;

&lt;p&gt;Acquires lock (after Thread 1 releases) ✅&lt;br&gt;
→ Now Thread 2 gets access to the critical section.&lt;/p&gt;

&lt;p&gt;Executes &lt;code&gt;getLogger()&lt;/code&gt;&lt;br&gt;
→ But this time, it sees &lt;code&gt;__loggerInstance&lt;/code&gt; is NOT &lt;code&gt;None&lt;/code&gt;, so it just returns the existing logger!&lt;/p&gt;
&lt;h2&gt;
  
  
  ✅ How This Prevents a Race Condition:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Without the lock:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Both threads could simultaneously check &lt;code&gt;__loggerInstance&lt;/code&gt; is None, and both might try to create it at the same time → ❌ Multiple instances (race condition!).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With the lock:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Only one thread enters the critical section at a time, ensuring only one logger instance is ever created.&lt;/p&gt;
&lt;h3&gt;
  
  
  Final output
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Logger Instantiated, Total number of instances -  1
Log from the first user
Log from the second user
All threads finished
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now as expected we only see one instance of the class gets instantiated, even in multi threaded environments&lt;/p&gt;
&lt;h2&gt;
  
  
  One last optimization
&lt;/h2&gt;

&lt;p&gt;This optimization is based on the fact that the usage of locks is expensive on the process. So we are gonna use it smartly. &lt;/p&gt;

&lt;p&gt;Notice one little small detail about our current &lt;code&gt;getLogger&lt;/code&gt; function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Returns the singleton instance, creates one if it doesn't exist
&lt;/span&gt;
        &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__mutexLock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

                &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Simulate delay
&lt;/span&gt;
                &lt;span class="c1"&gt;# Bypass __new__ and directly instantiate the class
&lt;/span&gt;                &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

                &lt;span class="c1"&gt;# Trigger __init__ manually on first creation
&lt;/span&gt;                &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the current implementation, we're acquiring a lock on every call to the &lt;code&gt;getLogger&lt;/code&gt; function, which is inefficient.&lt;/p&gt;

&lt;p&gt;However, locking is only necessary at the start of the program. Once the &lt;code&gt;__loggerInstance&lt;/code&gt; is set, any subsequent threads that access the &lt;code&gt;getLogger&lt;/code&gt; function won't create new instances—they'll simply receive the existing one.&lt;/p&gt;

&lt;p&gt;To optimize this, we'll add a small check before acquiring the lock to ensure it's only used during the initial instantiation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Returns the singleton instance, creates one if it doesn't exist
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# 🚀 Quick no-lock check
&lt;/span&gt;
            &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__mutexLock&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

                    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Simulate delay
&lt;/span&gt;
                    &lt;span class="c1"&gt;# Bypass __new__ and directly instantiate the class
&lt;/span&gt;                    &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

                    &lt;span class="c1"&gt;# Trigger __init__ manually on first creation
&lt;/span&gt;                    &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We added the above condition &lt;code&gt;if cls.__loggerInstance is None:&lt;/code&gt; to check if we are at the starting stages of the execution and then only acquire the lock to instantiate the class.&lt;/p&gt;

&lt;p&gt;This type of locking is called &lt;code&gt;Double-Checked Locking Pattern&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🪄 Why this is better:
&lt;/h2&gt;

&lt;p&gt;Most of the time (after the logger is created), the method will skip the lock completely 🙌&lt;/p&gt;

&lt;p&gt;Locking only happens once, during the first initialization&lt;/p&gt;

&lt;p&gt;Still 100% thread-safe ✅&lt;/p&gt;

&lt;p&gt;as tested by the below response from &lt;code&gt;main.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Logger Instantiated, Total number of instances -  1
Log from the first user
Log from the second user
All threads finished

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which is exactly the same that we got earlier&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing 🚀
&lt;/h2&gt;

&lt;p&gt;This is the ending of a short two part series on single ton design pattern. Here is the summary of what we covered in these two parts&lt;/p&gt;

&lt;p&gt;🔹 🧱 Base &lt;code&gt;Logger&lt;/code&gt; Setup &amp;amp; Instance Tracking: Implemented a basic Logger class, observed that multiple instances were created when used in different files, and added a static variable to track instance count.&lt;/p&gt;

&lt;p&gt;🔹 🔁 Converted Logger to Singleton: Restricted direct instantiation using &lt;code&gt;__new__&lt;/code&gt;, implemented a &lt;code&gt;getLogger()&lt;/code&gt; method to return a shared instance, ensuring only one Logger is created and reused across the application.&lt;/p&gt;

&lt;p&gt;🔹 🧵 (Thread-Safety): Highlighted the potential issue of multiple instances being created in a multithreaded environment and introduced the need for locks to make the Singleton thread-safe.&lt;/p&gt;

&lt;p&gt;🔹 Built a Thread-Safe Singleton Logger Class: Enhanced the original singleton pattern using &lt;code&gt;threading.Lock&lt;/code&gt; to ensure only one instance is created even in multithreaded environments, preventing race conditions.&lt;/p&gt;

&lt;p&gt;🔹 🔍 Explained &lt;code&gt;Threading&lt;/code&gt; vs &lt;code&gt;Multiprocessing&lt;/code&gt; in Python: Covered key differences, pros/cons, and why threading is the right choice here (I/O-bound task, not CPU-heavy).&lt;/p&gt;

&lt;p&gt;🔹 🧠 Applied Double-Checked Locking Optimization: Improved performance by avoiding unnecessary locking after the logger instance is initialized, maintaining thread safety with better efficiency.&lt;/p&gt;

&lt;h2&gt;
  
  
  About me ✌️
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://linktr.ee/bhavukkalra" rel="noopener noreferrer"&gt;Socials 🆔&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>Understanding Singleton Design Pattern with a Logger Example - Part 1</title>
      <dc:creator>Bhavuk kalra</dc:creator>
      <pubDate>Fri, 04 Apr 2025 11:42:37 +0000</pubDate>
      <link>https://dev.to/bhavukkalra/understanding-singleton-design-pattern-with-a-logger-example-4cen</link>
      <guid>https://dev.to/bhavukkalra/understanding-singleton-design-pattern-with-a-logger-example-4cen</guid>
      <description>&lt;p&gt;Written by &lt;a href="https://www.linkedin.com/in/bhavuk-kalra-256b67151/" rel="noopener noreferrer"&gt;Bhavuk Kalra&lt;/a&gt;✏️&lt;/p&gt;

&lt;p&gt;In this post, we will explore how the Singleton design pattern is implemented in code and demonstrate one of its most common use cases: a Logger.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is a Logger a Singleton?
&lt;/h2&gt;

&lt;p&gt;A logger should have a single instance that is shared across all files in an application. This ensures that all logs are stored in one place, making debugging and tracking events easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Overview
&lt;/h2&gt;

&lt;p&gt;Let's consider a real-world scenario where we have two different files performing separate tasks, but both need to log information. To achieve this, we will create a Logger class that follows the Singleton pattern, ensuring that all logs are written to the same instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Structure
&lt;/h2&gt;

&lt;p&gt;Our project will consist of three main files:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;logger.py&lt;/code&gt; – This file will contain the Singleton Logger class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;user1.py&lt;/code&gt; and &lt;code&gt;user2.py&lt;/code&gt; – These files represent different parts of the application that will use the logger.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;main.py&lt;/code&gt; – This file acts as the central server, calling &lt;code&gt;user1.py&lt;/code&gt; and &lt;code&gt;user2.py&lt;/code&gt; to execute their respective processes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After setting up, the directory structure will 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;└── root/
    ├── user1.py
    ├── user2.py
    ├── logger.py
    ├── main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the next steps, we will implement the Singleton pattern for our logger and see it in action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Boilerplate Code
&lt;/h2&gt;

&lt;p&gt;Now, let's set up the basic functionality of our project. We'll define the Logger class and ensure that all logs are recorded using a single instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up the Logger
&lt;/h2&gt;

&lt;p&gt;We’ll start by defining the Logger class in &lt;code&gt;logger.py&lt;/code&gt;. This class will ensure that only one instance exists throughout the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  📄 &lt;code&gt;logger.py&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Logger Instantiated&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the Logger in Different Files
&lt;/h2&gt;

&lt;p&gt;Both &lt;code&gt;user1.py&lt;/code&gt; and &lt;code&gt;user2.py&lt;/code&gt; will import and use the &lt;code&gt;Logger&lt;/code&gt; class to log messages corresponding to two different processes.&lt;/p&gt;

&lt;h2&gt;
  
  
  📄 &lt;code&gt;user1.py&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;doProcessingUser1&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Log from the first user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  📄 &lt;code&gt;user2.py&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;doProcessingUser2&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Log from the second user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Entry Point:
&lt;/h2&gt;

&lt;h2&gt;
  
  
  📄 &lt;code&gt;main.py&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;user1&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;doProcessingUser1&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;user2&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;doProcessingUser2&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;doProcessingUser1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;doProcessingUser2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets run the &lt;code&gt;main.py&lt;/code&gt; file to check if our base setup is working.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Output&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Logger Instantiated
Log from the first user
Logger Instantiated
Log from the second user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Logger Functionality 📝
&lt;/h2&gt;

&lt;p&gt;For our Logger, we need to ensure:&lt;/p&gt;

&lt;p&gt;🔹 Single Instance: Only one instance of the Logger class exists throughout the project.&lt;br&gt;
🔹 Restricted Instantiation: Other processes must not create additional instances of the Logger.&lt;/p&gt;

&lt;p&gt;With this setup, all logs will be managed by a single, central Logger instance, ensuring consistency and efficiency! 🚀&lt;/p&gt;
&lt;h2&gt;
  
  
  Tracking Logger Instances for Debugging 🧐
&lt;/h2&gt;

&lt;p&gt;To ensure that our Singleton implementation is working correctly, we will track how many instances of the Logger class have been created. This will help us debug and verify that only one instance is used throughout the application.&lt;/p&gt;
&lt;h3&gt;
  
  
  How Will We Track Instances? 📊
&lt;/h3&gt;

&lt;p&gt;🔹 Use a Static Variable – This will keep track of the number of instances created.&lt;br&gt;
🔹 Why a Static Variable? – Unlike instance variables (which belong to a specific object), static variables belong to the class itself and are shared across all instances.&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementing Instance Tracking 🔍
&lt;/h3&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Add a static variable&lt;/strong&gt; to count the number of instances created.&lt;br&gt;
2️⃣&lt;strong&gt;Update the constructor&lt;/strong&gt; to increment this counter whenever a new instance is attempted.&lt;br&gt;
3️⃣ &lt;strong&gt;Log the instance&lt;/strong&gt; count for verification.&lt;/p&gt;

&lt;p&gt;With these changes, we’ll be able to confirm whether our Singleton pattern is correctly restricting multiple instances! 🚀&lt;/p&gt;

&lt;p&gt;Since our Logger class should ideally have only one instance, we will implement functionality to print how many instances have been created. This will help us verify that the Singleton pattern is correctly enforced.&lt;/p&gt;
&lt;h3&gt;
  
  
  Updating the Codebase 🛠️
&lt;/h3&gt;

&lt;p&gt;We implement a static counter belonging to a specific class and shared across all instances. &lt;/p&gt;
&lt;h2&gt;
  
  
  📄 &lt;code&gt;logger.py&lt;/code&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;# private Static variable to track num of instances created
&lt;/span&gt;    &lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="c1"&gt;# private:
&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Logger Instantiated, Total number of instances - &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We have now introduced a static variable &lt;code&gt;numInstances&lt;/code&gt; to keep track of how many instances of the Logger class have been created across the codebase.&lt;/p&gt;

&lt;p&gt;Lets run the &lt;code&gt;main.py&lt;/code&gt; to see if what we get&lt;/p&gt;
&lt;h2&gt;
  
  
  📄 &lt;code&gt;output&lt;/code&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Logger Instantiated, Current number of instances -  1
Log from the first user
Logger Instantiated, Current number of instances -  2
Log from the second user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;which is an expected output as we are here creating two instances of &lt;code&gt;Logger&lt;/code&gt; class in two different files namely &lt;code&gt;user1.py&lt;/code&gt; and &lt;code&gt;user2.py&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Converting to a Singleton Design Pattern 🏗️
&lt;/h2&gt;

&lt;p&gt;To implement the Singleton design pattern for our Logger, we need to ensure that only one instance of the class is created and shared across the entire application.&lt;/p&gt;
&lt;h2&gt;
  
  
  Steps to Implement Singleton ✅
&lt;/h2&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Make the Constructor Private&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prevents the creation of new instances directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;Create a Static Instance&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Stores the single instance of the class.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ensures all users access the same instance.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3️⃣ &lt;strong&gt;Provide a Static Method for Access&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This method will instantiate the static instance if it hasn’t been created yet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All users will access the same instance through this method.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Implementation 🛠️
&lt;/h2&gt;
&lt;h2&gt;
  
  
  Making the constructor private 🔐
&lt;/h2&gt;

&lt;p&gt;Since the constructor is private, no one can create multiple instances of the class. However, we still need at least one instance that other users can access, right?&lt;/p&gt;

&lt;p&gt;What we can do is &lt;strong&gt;create a static function&lt;/strong&gt; that returns the instance of the Logger class.&lt;/p&gt;

&lt;p&gt;(Read that again if you have to so that you make sense of it)&lt;/p&gt;

&lt;p&gt;Since Python is a very flexible language, we can't directly make a class constructor private like in C++ or Java.&lt;/p&gt;

&lt;p&gt;Instead, we will use &lt;code&gt;Name Mangling&lt;/code&gt; to achieve this.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using &lt;code&gt;__new__&lt;/code&gt; with Name Mangling
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;__new__()&lt;/code&gt; method is called before &lt;code&gt;__init__()&lt;/code&gt; and controls the instance creation.&lt;/p&gt;

&lt;p&gt;By altering &lt;code&gt;__new__&lt;/code&gt; method we are going to restrict the access to the &lt;code&gt;__init__&lt;/code&gt; method. Raising an error every time a method tries to directly instantiate it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;# private Static variable to track num of instances created
&lt;/span&gt;    &lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="c1"&gt;# private static variable to denote if instance was created
&lt;/span&gt;    &lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Private constructor using __new__ to prevent direct instantiation
&lt;/span&gt;        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Use getLlogger() to create an instance.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Logger Instantiated, Total number of instances - &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__numInstances&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nd"&gt;@classmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Returns the singleton instance, creates one if it doesn't exist
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Bypass __new__ and directly instantiate the class
&lt;/span&gt;            &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;__new__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

             &lt;span class="c1"&gt;# Trigger __init__ manually on first creation
&lt;/span&gt;            &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__loggerInstance&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the &lt;code&gt;@classmethod&lt;/code&gt; decorator to pass the caller class (&lt;code&gt;cls&lt;/code&gt;) into the function. This makes our code safer in cases of inheritance and polymorphism.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding &lt;code&gt;super(Logger, cls)&lt;/code&gt; 🔄
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;super(Logger, cls)&lt;/code&gt; returns a &lt;strong&gt;proxy object&lt;/strong&gt; that allows access to the parent class methods.&lt;/p&gt;

&lt;p&gt;🔹 Logger – The current class where we start the lookup.&lt;br&gt;
🔹 &lt;code&gt;cls&lt;/code&gt; – The class whose instance we want to create.&lt;br&gt;
🔹 This ensures that if we use inheritance, Python creates an instance of the correct class.&lt;/p&gt;
&lt;h2&gt;
  
  
  How super(Logger, cls).&lt;strong&gt;new&lt;/strong&gt;(cls) Works ⚙️
&lt;/h2&gt;

&lt;p&gt;🔹 &lt;code&gt;__new__(cls)&lt;/code&gt; is a special method that creates a new instance of a class before initializing it.&lt;br&gt;
🔹 Python looks for the parent class of &lt;code&gt;Logger&lt;/code&gt; (which is &lt;code&gt;object&lt;/code&gt;).&lt;br&gt;
🔹 It then calls &lt;code&gt;object.__new__(cls)&lt;/code&gt;, ensuring that an instance of cls (either Logger or a subclass) is created properly.&lt;/p&gt;

&lt;p&gt;This approach keeps our &lt;strong&gt;Singleton pattern safe and flexible&lt;/strong&gt; when used with &lt;strong&gt;inheritance&lt;/strong&gt;! &lt;/p&gt;
&lt;h2&gt;
  
  
  📄 &lt;code&gt;user1.py&lt;/code&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;doProcessingUser1&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Log from the first user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  📄 &lt;code&gt;user2.py&lt;/code&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;doProcessingUser2&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getLogger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Log from the second user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Logger Instantiated, Total number of instances -  1
Log from the first user
Log from the second user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔍 Observe the Singleton in Action
&lt;/h2&gt;

&lt;p&gt;Even with two &lt;code&gt;getLogger()&lt;/code&gt; calls, we still have only one instance of the &lt;code&gt;Logger&lt;/code&gt; class being used. ✅&lt;/p&gt;

&lt;h3&gt;
  
  
  🚫 What Happens if Someone Tries to Instantiate the Logger?
&lt;/h3&gt;

&lt;p&gt;Now that we have &lt;strong&gt;restricted direct instantiation&lt;/strong&gt;, let’s imagine a scenario where a user tries to create a new instance manually.&lt;/p&gt;

&lt;p&gt;To test this, we will modify our code and see what happens when someone attempts to instantiate the &lt;code&gt;Logger&lt;/code&gt; class directly. 🛠️&lt;/p&gt;

&lt;h2&gt;
  
  
  📄 &lt;code&gt;user1.py&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Logger&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;doProcessingUser1&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;loggerUser1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Log from the first user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get this error, which is our custom exception.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;raise Exception("Use getLlogger() to create an instance.")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✅ First Step of the Singleton Pattern Completed!
&lt;/h2&gt;

&lt;p&gt;But are we completely done? No! 🚫&lt;/p&gt;

&lt;h3&gt;
  
  
  ⚠️ What’s the Next Challenge? (Hint: Multi-threading) 🧵
&lt;/h3&gt;

&lt;p&gt;Our current implementation is not &lt;strong&gt;thread-safe&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  🧐 What’s the Problem?
&lt;/h3&gt;

&lt;p&gt;Imagine a scenario where two threads call &lt;code&gt;getLogger()&lt;/code&gt; at the same time:&lt;/p&gt;

&lt;p&gt;🔹 Both threads check if &lt;code&gt;__loggerInstance&lt;/code&gt; is &lt;code&gt;None&lt;/code&gt;.&lt;br&gt;
🔹 Since both see it as &lt;code&gt;None&lt;/code&gt;, they &lt;strong&gt;both try to create an instance&lt;/strong&gt;.&lt;br&gt;
🔹 As a result, two instances of &lt;code&gt;Logger&lt;/code&gt; are created— **breaking **our Singleton pattern! ❌&lt;/p&gt;

&lt;h2&gt;
  
  
  🔒 Next Step: Making the Code Thread-Safe
&lt;/h2&gt;

&lt;p&gt;In the next part, we will:&lt;/p&gt;

&lt;p&gt;✔️ Explore different approaches to handling multi-threading.&lt;br&gt;
✔️ Identify corner cases to watch out for.&lt;br&gt;
✔️ Learn how to use mutex locks efficiently to prevent race conditions.&lt;/p&gt;

&lt;p&gt;Stay tuned as we enhance our Singleton implementation! 🚀&lt;/p&gt;

&lt;p&gt;All this good stuff in the part 2 of this tiny two part series on &lt;strong&gt;Singleton Design Pattern&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  About me ✌️
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://linktr.ee/bhavukkalra" rel="noopener noreferrer"&gt;Socials 🆔&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>designpatterns</category>
      <category>programming</category>
      <category>design</category>
    </item>
    <item>
      <title>⚛️How to create a React Web Application without create-react-app CLI</title>
      <dc:creator>Bhavuk kalra</dc:creator>
      <pubDate>Thu, 11 Apr 2024 12:21:59 +0000</pubDate>
      <link>https://dev.to/bhavukkalra/how-to-create-a-react-web-application-without-create-react-app-cli-5f62</link>
      <guid>https://dev.to/bhavukkalra/how-to-create-a-react-web-application-without-create-react-app-cli-5f62</guid>
      <description>&lt;p&gt;In this post we'll build a React web application using webpack, Babel, React, and React DOM without relying on create-react-app&lt;/p&gt;

&lt;h2&gt;
  
  
  ❓What is create-react-app
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;create-react-app is a CLI provided by facebook to make it easier for users to get started building with React. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It is ideal for beginners to run a single command to get your environment ready to get started, but it has two major flaws&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It &lt;strong&gt;restricts customization&lt;/strong&gt; for build configuration. For example you might want to use SWC (Speedy Web Compiler) for your deployment pipeline instead of pre-installed Babel for compilation of JS/TS codebase.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It &lt;strong&gt;abstracts a lot of the inbuilt tools&lt;/strong&gt; that makes React functional like processes of Transpilation, Minification, Bundling which one should know about while making Web applications&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ℹ️ Some Terminologies
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Transpiler&lt;/strong&gt; - Converts one form of JS code to another form of JS code. Convert ES6(ECMA Script 2015) into code that browsers can understand. Example &lt;strong&gt;Babel&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Module Bundler&lt;/strong&gt; - Takes different files HTML,JS, Style Sheets(CSS). And bundles them into a single file. Helps managing dependencies relations (Including loading modules in correct order), To improve performance, To organize your code, To reduce the number of HTTP requests Example &lt;strong&gt;Webpack, Parcel, and Rollup.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minification&lt;/strong&gt; - The process of removing unnecessary characters from programming languages or markup languages without changing its functionality. It is also known as minimization. Minification can include: Removing whitespace, Removing comments, Removing semicolons, Using shorter variable and function names, and Removing unused code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  🛠️ Tools used
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Babel&lt;/strong&gt; - A &lt;strong&gt;transpiler&lt;/strong&gt;that converts &lt;strong&gt;JSX -&amp;gt; JS, JS(ES6) -&amp;gt; JS(ES5)&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Webpack&lt;/strong&gt; - A &lt;strong&gt;module bundler&lt;/strong&gt;. Converts all assets to single file which is code optimized, minified.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;This combination allows developers to write code using the latest language features and ensure it's efficiently packaged for deployment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  ❓Why use so many tools required to run react locally
&lt;/h2&gt;

&lt;p&gt;Before we start with the walkthrough there is one important question we need to answer&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;why do we need these specific tools such as vite or node(npm) to run React locally. Why can't we just use normal HTML, CSS and JS?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we use JSX(Javascript XML) syntax very frequently which can't be understood by the browser. We need the presence of special build tools (Here Babel for Transpiling JSX into JS)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
Function returning an HTML tag giving an error.
For using this type of syntax inside react we need a 
special build tool Babel (a Transpiler)
*/&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;)&lt;/span&gt;   
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output -
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5uliazx44u6456abb14h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5uliazx44u6456abb14h.png" alt="Output of above code on Web page console" width="516" height="250"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For converting JS files using the syntax of JSX to the files that can be understood by the browser, &lt;/p&gt;

&lt;p&gt;We need special setup like &lt;strong&gt;Vite&lt;/strong&gt; etc (Includes Build tools which Transforms and optimizes code eg remove unnecessary whitespaces, Handles imports etc)&lt;/p&gt;

&lt;h2&gt;
  
  
  Note🗒️
&lt;/h2&gt;

&lt;p&gt;You can skip the setup of having build tools by not using JSX syntax. &lt;strong&gt;You can use React without JSX if you don't want to set up compilation in your build environment&lt;/strong&gt;. However Writing React without JSX can be more verbose than writing React with JSX.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;We have built our base fundamentals and answered some cruicial questions&lt;/u&gt;. Now we are ready to start coding ⌨️&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets start coding 🖮
&lt;/h2&gt;

&lt;p&gt;Open your terminal and run the following commands in order as they appear.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initiates the NPM project with default configs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the React library (Ofcourse)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This package &lt;strong&gt;acts as a glue between React and the webpages DOM&lt;/strong&gt;. DOM is just a programming Interface for webpages. To know more about on what is it and how its useful in web-dev click here &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction"&gt;MDN DOM&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🧊 Install webpack and its dependencies
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i webpack --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install our module bundler &lt;strong&gt;webpack&lt;/strong&gt;. Only needed as a dev dependency and not while our app is running on production.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i webpack-cli --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Provides a faster way  to initialize and configure a webpack project through the cli. (Also watches for file changes).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i webpack-dev-server --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A utility complementing webpack. Provides a single line to setup a Live reload dev server using webpack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i html-webpack-plugin --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simplifies HTML file creation to serve our webpack bundles. Assists with adding our bundled files to &lt;code&gt;index.html&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;🅱️&lt;/strong&gt; Install Babel and its utilities
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @babel/core --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main core babel transpiler&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i babel-loader --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Loader&lt;/strong&gt; - Is what webpack uses to handle and process different file types. It dictates how certain file types are processed as they are imported into the project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case transpile the js files from ES6 to ES5 before minifying them&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;As there are different files hence there are difference loaders&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.npmjs.com/package/babel-loader"&gt;babel-loader&lt;/a&gt; - For JS files. This package allows transpiling JavaScript files using Babel and webpack.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.npmjs.com/package/sass-loader"&gt;sass-loader&lt;/a&gt; - Transpiles SAAS files to css&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.npmjs.com/package/style-loader"&gt;style-loader&lt;/a&gt; - style-loader takes CSS you've imported in your JavaScript files, and injects them as &lt;strong&gt;Style&lt;/strong&gt; tags into the DOM. It's particularly useful for inlining Critical CSS into the &lt;strong&gt;Head&lt;/strong&gt; tag of your page.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @babel/preset-react --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Preset is a set of plugins that support language features. By default Babel has the plugin ES2015 which add support for ES5 javascript&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;preset-react&lt;/strong&gt; - Adds support for JSX(Javascript XML)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @babel/preset-env --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;preset-env is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s). This both makes your life easier and JavaScript bundles smaller!&lt;/p&gt;

&lt;h3&gt;
  
  
  Contents of &lt;code&gt;package.json&lt;/code&gt; After installing all modules.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"create-react-app"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Error: no test specified&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"keywords"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"react"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^18.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"react-dom"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^18.2.0"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@babel/core"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.24.4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@babel/preset-env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.24.4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@babel/preset-react"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.24.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"babel-loader"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^9.1.3"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"html-webpack-plugin"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.6.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"webpack"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.91.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"webpack-cli"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.1.4"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"webpack-dev-server"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^5.0.4"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your installed versions might be different(&lt;strong&gt;higher&lt;/strong&gt;) if you are following this at a later time.&lt;/p&gt;

&lt;p&gt;After the above steps your directory structure should 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;└── root/
    ├── node_modules/
    ├── package.json
    ├── package-lock.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configure Webpack ⚒️
&lt;/h2&gt;

&lt;p&gt;Create &lt;code&gt;webpack.config.js&lt;/code&gt; in the root directory and add the following content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// For injecting the generated minified bundle.js to the index.html&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HTMLWebPackPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;html-webpack-plugin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./src/index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/dist&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bundle.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;

  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HTMLWebPackPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./src/index.html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;

  &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/.js$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/node_modules/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;use&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;babel-loader&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;presets&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@babel/preset-env&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@babel/preset-react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;entry&lt;/strong&gt;- Essentially tell webpack which file to use as as the root to create a dependency graph. used to resolve modules which are dependent on one another and also building the modules in order (For example build those modules first which are required as a dependency by other modules)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;output&lt;/strong&gt; - As webpack is a module bundler. It takes in a group files and outputs a smaller minified group of files. We need to specify the final output javascript file name and its path that will be generated by webpack&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;module&lt;/strong&gt; - Informs webpack what kind of loader we'll be using. It holds the information on how the different kind of modules will be treated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;rules&lt;/strong&gt; - Informs webpack that we'll be using 'BABEL' to transpile our js files. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;test&lt;/strong&gt; - Which files would be covered under this rule. It's stated by the regex &lt;code&gt;/.js$/&lt;/code&gt; i.e all files with extension js.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;exclude&lt;/strong&gt; - Excludes &lt;code&gt;node_modules&lt;/code&gt; from transpilation process. If we don't exclude those, Otherwise it Makes the process slower and also we don't need to transpile those as they are mostly already minified. Only under very specific conditions we want to transpile some of the &lt;code&gt;node_modules&lt;/code&gt; but not in this case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;use&lt;/strong&gt; - Specifies the babel-loader' that would be used for transpiling. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;options&lt;/strong&gt; - Specifies the presets which would extend the loaders ability to intake JSX and ES6 syntax &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  👓 Current directory structure
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;└── root/
    ├── node_modules/
    ├── package.json
    ├── package-lock.json
    ├── webpack.config.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✍️ Create the files required
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create &lt;code&gt;index.js&lt;/code&gt;(root for webpack to create a dependency graph) in the &lt;code&gt;src&lt;/code&gt; folder and add the contents.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Get the component from the file to be rendered (Will be created later)&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./components/App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;// Get root id from index.html (will be created later)&lt;/span&gt;
&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a folder &lt;code&gt;components&lt;/code&gt; inside &lt;code&gt;src&lt;/code&gt; and a file &lt;code&gt;App.js&lt;/code&gt; to store the component that will be rendered in &lt;code&gt;root&lt;/code&gt; div
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/*
Extending the Component class gives us access to its life-cycle method one of which is render
*/&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;world&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Inside the &lt;code&gt;src&lt;/code&gt; folder create 'index.html'
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Document&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- Where are react component will be renderer --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  👓 Current directory structure
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;└── root/
    ├── node_modules/
    ├── package.json
    ├── package-lock.json
    ├── webpack.config.js
    ├── src/
    │   ├── components/
    │   |    ├── App.js
    |   ├── index.html
    |   ├── index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ➕ Add scripts to package.json
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"start": "webpack-dev-server --mode development --open --hot"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are using &lt;code&gt;webpack-dev-server&lt;/code&gt; for live reload. It provides a fast in-memory access to the webpack assets to accomplish this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flags&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;--mode development&lt;/code&gt; - Enable production optimizations or development hints.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;--hot&lt;/code&gt; - Enables Hot Module Replacement(HMR).It exchanges, adds, or removes modules while an application is running, without a full reload&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;--open&lt;/code&gt; - Open the application in a new tab&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"build": "webpack --mode production"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will create a production build for us to deploy. Bundle and minify all our js in &lt;code&gt;bundle.js&lt;/code&gt; located on a folder &lt;code&gt;dist&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;HTMLWebpackPlugin&lt;/strong&gt; will inject our output &lt;code&gt;bundle.js&lt;/code&gt; in index.html&lt;/p&gt;

&lt;h2&gt;
  
  
  Final content for &lt;code&gt;package.json&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;create-react-app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;version&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1.0.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;main&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;index.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scripts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack-dev-server --mode development --open --hot&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;build&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack --mode production&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;keywords&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;author&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;license&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ISC&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dependencies&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^18.2.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^18.2.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;devDependencies&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@babel/core&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^7.24.4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@babel/preset-env&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^7.24.4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@babel/preset-react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^7.24.1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;babel-loader&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^9.1.3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;html-webpack-plugin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^5.6.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^5.91.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack-cli&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^5.1.4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;webpack-dev-server&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;^5.0.4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ♾ Run the scripts
&lt;/h2&gt;

&lt;p&gt;To start a development live reload server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Logs
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F538q07qp885gsps9mwct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F538q07qp885gsps9mwct.png" alt="sadasdasd" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Output
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgoytb0y5hv9kcner28ql.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgoytb0y5hv9kcner28ql.png" alt="Logs of npm run start" width="263" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Try it on your own
&lt;/h4&gt;

&lt;p&gt;Make an edit to &lt;code&gt;index.html&lt;/code&gt; and change its contents from "Hello World!" to "Hello Dev.to!". Save the file and see if the page gets updated as soon as you save your file. This is &lt;strong&gt;Hot Reload&lt;/strong&gt; provided by the module &lt;code&gt;webpack-dev-server&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will create a &lt;code&gt;dist&lt;/code&gt; production build folder with minified &lt;code&gt;bundle.js&lt;/code&gt; generated by webpack and &lt;code&gt;index.html&lt;/code&gt; (Already injected with &lt;code&gt;bundle.js&lt;/code&gt; thanks to the module &lt;code&gt;html-webpack-plugin&lt;/code&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  👓 Current directory structure
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;└── root/
    ├── dist/
    |   ├── index.html
|   |   ├── bundle.js
    ├── node_modules/
    ├── package.json
    ├── package-lock.json
    ├── webpack.config.js
    ├── src/
    │   ├── components/
    │   |    ├── App.js
    |   ├── index.html
    |   ├── index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  EXTRAS
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Why use require("path") which is a Node function inside frontend development?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you try to run &lt;code&gt;const fs = require("fs")&lt;/code&gt; on browser it will throw an error. meaning its a Node enabled function.&lt;/p&gt;

&lt;p&gt;Its a common practice in the development cycle. Whenever there is a need for local control(&lt;code&gt;BUT ONLY DURING THE DEVELOPMENT PROCESS&lt;/code&gt;) for handling local files, paths, creating servers locally to enable faster development. We use local Node runtime environment(Which also uses JS as coding language).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Node version?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This tutorial was made on node version 20.12.2 LTS&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing✌️
&lt;/h2&gt;

&lt;p&gt;Thanks for reading it thus far. This is my first time publishing to dev.to. Got recommended this website by a fellow developer, Let's see how it goes.&lt;/p&gt;

&lt;h2&gt;
  
  
  About me
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://linktr.ee/bhavukkalra"&gt;Socials 🆔&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
