<?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: Krati Joshi</title>
    <description>The latest articles on DEV Community by Krati Joshi (@joshikrati03).</description>
    <link>https://dev.to/joshikrati03</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4040583%2F7d30ee5e-5124-4c74-8ef9-8fb626646302.png</url>
      <title>DEV Community: Krati Joshi</title>
      <link>https://dev.to/joshikrati03</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joshikrati03"/>
    <language>en</language>
    <item>
      <title>Node.js Internals #1: Why require() Doesn't Run Your File Again</title>
      <dc:creator>Krati Joshi</dc:creator>
      <pubDate>Tue, 21 Jul 2026 18:21:08 +0000</pubDate>
      <link>https://dev.to/joshikrati03/nodejs-internals-1-why-require-doesnt-run-your-file-again-5dfb</link>
      <guid>https://dev.to/joshikrati03/nodejs-internals-1-why-require-doesnt-run-your-file-again-5dfb</guid>
      <description>&lt;h2&gt;
  
  
  🤔 Have you ever noticed this?
&lt;/h2&gt;

&lt;p&gt;Have you ever noticed this?&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;express&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;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express2&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;express&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;p&gt;You called &lt;code&gt;require()&lt;/code&gt; twice... but Node.js didn't execute the package twice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why? 🤔&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you borrow a book from a library.&lt;/p&gt;

&lt;p&gt;The librarian searches the shelf once, hands you the book, and remembers where it is. When someone else asks for the same book, they don't search the entire library again.&lt;/p&gt;

&lt;p&gt;Node.js works in a similar way.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;first&lt;/strong&gt; time you call:&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="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;./counter&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;p&gt;Node.js:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📂 Finds the file&lt;/li&gt;
&lt;li&gt;⚙️ Executes it&lt;/li&gt;
&lt;li&gt;📦 Stores the exported object in memory (&lt;strong&gt;module cache&lt;/strong&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;next&lt;/strong&gt; time you call:&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="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;./counter&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;p&gt;It simply returns the cached module instead of executing the file again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's prove it 👇
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// counter.js&lt;/span&gt;
&lt;span class="nx"&gt;console&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Module Loaded&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&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;./counter&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&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;./counter&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;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;Module Loaded
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;require()&lt;/code&gt; is called twice, the module executes only once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why does this matter?
&lt;/h2&gt;

&lt;p&gt;Imagine your application has &lt;strong&gt;200 files&lt;/strong&gt;, and most of them import Express.&lt;/p&gt;

&lt;p&gt;Without module caching, Node.js would execute Express hundreds of times.&lt;/p&gt;

&lt;p&gt;Instead, it loads Express &lt;strong&gt;once&lt;/strong&gt; and reuses the cached module everywhere, improving performance and reducing unnecessary work.&lt;/p&gt;

&lt;h2&gt;
  
  
  📌 Key Takeaway
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;require()&lt;/code&gt; doesn't execute a module every time. It executes it &lt;strong&gt;once&lt;/strong&gt;, caches it, and returns the cached version on future calls.&lt;/p&gt;




&lt;p&gt;💬 &lt;strong&gt;Question for you:&lt;/strong&gt; Before reading this, did you expect &lt;code&gt;require()&lt;/code&gt; to execute the file every time?&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Next in Backend Internals:&lt;/strong&gt;&lt;br&gt;
🔍 What actually happens inside &lt;code&gt;require()&lt;/code&gt; before your code even starts running? 👀&lt;/p&gt;

</description>
      <category>backenddevelopment</category>
      <category>node</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
