<?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: Sudhanshu code</title>
    <description>The latest articles on DEV Community by Sudhanshu code (@sudhanshu_code_b8ef988ceb).</description>
    <link>https://dev.to/sudhanshu_code_b8ef988ceb</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%2F4021146%2Fa105ebff-e4c0-4dbb-8df9-96ba4a96061f.jpeg</url>
      <title>DEV Community: Sudhanshu code</title>
      <link>https://dev.to/sudhanshu_code_b8ef988ceb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sudhanshu_code_b8ef988ceb"/>
    <language>en</language>
    <item>
      <title># From JavaScript to Node.js: Understanding What Really Happens Behind the Scenes (Part 4.3A.1)</title>
      <dc:creator>Sudhanshu code</dc:creator>
      <pubDate>Fri, 24 Jul 2026 12:17:23 +0000</pubDate>
      <link>https://dev.to/sudhanshu_code_b8ef988ceb/-from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-1o8i</link>
      <guid>https://dev.to/sudhanshu_code_b8ef988ceb/-from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-1o8i</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;# Module Resolution Algorithm (Part 1): How Node.js Finds the Right Module&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the previous article, we explored one of the most fascinating parts of Node.js—the hidden Module Wrapper Function. We learned that every CommonJS module is wrapped inside a function before execution, and we also discovered that &lt;code&gt;require()&lt;/code&gt; is &lt;strong&gt;not&lt;/strong&gt; a JavaScript feature. It is provided by the Node.js runtime.&lt;/p&gt;

&lt;p&gt;But a very important mystery still remains.&lt;/p&gt;

&lt;p&gt;When we write:&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;fs&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;fs&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;or&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;math&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;./math&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;how does Node.js know where these modules are located?&lt;/p&gt;

&lt;p&gt;How does it decide whether &lt;code&gt;"fs"&lt;/code&gt; is a built-in module or a file inside your project?&lt;/p&gt;

&lt;p&gt;Why does &lt;code&gt;require("./math")&lt;/code&gt; work even if you don't write &lt;code&gt;.js&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;And what happens internally before your code starts executing?&lt;/p&gt;

&lt;p&gt;The answer lies inside one of Node.js's most important systems:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The Module Resolution Algorithm&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Understanding this algorithm is essential because &lt;strong&gt;every Node.js application uses it hundreds or even thousands of times while starting.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  What is Module Resolution?
&lt;/h1&gt;

&lt;p&gt;The word &lt;em&gt;resolution&lt;/em&gt; simply means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Finding the actual file represented by the string passed to &lt;code&gt;require()&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose you write:&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;./math&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;To you, &lt;code&gt;"./math"&lt;/code&gt; looks like a file.&lt;/p&gt;

&lt;p&gt;But for Node.js, it is initially nothing more than a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"./math"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node cannot execute a string.&lt;/p&gt;

&lt;p&gt;It needs the real file.&lt;/p&gt;

&lt;p&gt;So its first job is to answer one question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Which exact file should I load?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The complete process of converting the string inside &lt;code&gt;require()&lt;/code&gt; into an actual file on disk is called &lt;strong&gt;Module Resolution&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Does Node Need a Resolution Algorithm?
&lt;/h1&gt;

&lt;p&gt;Imagine a project 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;project/

├── app.js
├── math.js
├── database.js
├── auth.js
└── utils/
    ├── logger.js
    └── helper.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now look at these statements.&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;./math&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;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;./database&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;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;./utils/logger&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;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;fs&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;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;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;All of them look similar.&lt;/p&gt;

&lt;p&gt;But internally they are completely different.&lt;/p&gt;

&lt;p&gt;Some point to your own files.&lt;/p&gt;

&lt;p&gt;Some point to Node's built-in modules.&lt;/p&gt;

&lt;p&gt;Some point to packages installed using npm.&lt;/p&gt;

&lt;p&gt;Node cannot treat them all the same.&lt;/p&gt;

&lt;p&gt;It must identify what type of module you are requesting before loading it.&lt;/p&gt;

&lt;p&gt;This decision-making process is the first stage of Module Resolution.&lt;/p&gt;




&lt;h1&gt;
  
  
  Three Types of Modules
&lt;/h1&gt;

&lt;p&gt;Node.js classifies modules into three categories.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Modules

│

├── Core Modules

├── Local Modules

└── Third-Party Modules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every &lt;code&gt;require()&lt;/code&gt; call belongs to one of these categories.&lt;/p&gt;

&lt;p&gt;Let's understand each one.&lt;/p&gt;




&lt;h1&gt;
  
  
  1. Core Modules
&lt;/h1&gt;

&lt;p&gt;Core Modules are modules that come bundled with Node.js itself.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;https&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;path&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;crypto&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;stream&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;events&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;os&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;url&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;zlib&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;buffer&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These modules are already part of the Node.js runtime.&lt;/p&gt;

&lt;p&gt;You never install them.&lt;/p&gt;

&lt;p&gt;You simply write:&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;fs&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;fs&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;and Node immediately understands what you mean.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Are They Called Core Modules?
&lt;/h1&gt;

&lt;p&gt;Because they are part of Node's core source code.&lt;/p&gt;

&lt;p&gt;If you install Node.js today,&lt;/p&gt;

&lt;p&gt;modules like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fs

path

http

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

&lt;/div&gt;



&lt;p&gt;are already present inside the runtime.&lt;/p&gt;

&lt;p&gt;This is why commands like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;fs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;make no sense.&lt;/p&gt;

&lt;p&gt;The module already exists.&lt;/p&gt;




&lt;h1&gt;
  
  
  How Does Node Recognize a Core Module?
&lt;/h1&gt;

&lt;p&gt;Suppose your program contains:&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;fs&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 starts its resolution process.&lt;/p&gt;

&lt;p&gt;First it checks:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"Is this the name of a Core Module?"&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the answer is YES...&lt;/p&gt;

&lt;p&gt;the search stops immediately.&lt;/p&gt;

&lt;p&gt;Node loads the internal implementation.&lt;/p&gt;

&lt;p&gt;No filesystem search occurs.&lt;/p&gt;

&lt;p&gt;No &lt;code&gt;node_modules&lt;/code&gt; lookup occurs.&lt;/p&gt;

&lt;p&gt;No disk traversal happens.&lt;/p&gt;

&lt;p&gt;The module is loaded directly.&lt;/p&gt;

&lt;p&gt;Conceptually the flow looks 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;require("fs")

↓

Core Module?

↓

YES

↓

Load Internal Implementation

↓

Return module.exports
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one reason Core Modules load very quickly.&lt;/p&gt;




&lt;h1&gt;
  
  
  Is &lt;code&gt;fs&lt;/code&gt; Written in JavaScript?
&lt;/h1&gt;

&lt;p&gt;Not entirely.&lt;/p&gt;

&lt;p&gt;Many Core Modules are implemented using a combination of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript&lt;/li&gt;
&lt;li&gt;C++&lt;/li&gt;
&lt;li&gt;Native Operating System APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;looks like an ordinary JavaScript function.&lt;/p&gt;

&lt;p&gt;But internally the request travels through:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript

↓

Node API

↓

C++ Binding

↓

libuv

↓

Operating System

↓

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

&lt;/div&gt;



&lt;p&gt;We've already studied this architecture in Part 2.&lt;/p&gt;

&lt;p&gt;Module Resolution simply decides &lt;strong&gt;which module should receive your request.&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  2. Local Modules
&lt;/h1&gt;

&lt;p&gt;Local Modules are modules that belong to your own project.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/

├── app.js

└── math.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside &lt;code&gt;app.js&lt;/code&gt;:&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;math&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;./math&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;Notice something important.&lt;/p&gt;

&lt;p&gt;The path begins with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That small symbol completely changes Node's behavior.&lt;/p&gt;

&lt;p&gt;Instead of checking Core Modules,&lt;/p&gt;

&lt;p&gt;Node immediately understands:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"This module is inside the current project."&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  What Does &lt;code&gt;./&lt;/code&gt; Mean?
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;./&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Current Directory&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose your project looks 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;project/

├── app.js

└── math.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Current file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Current directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Therefore:&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;./math&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;means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/math
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node now begins searching for the file.&lt;/p&gt;




&lt;h1&gt;
  
  
  Parent Directory
&lt;/h1&gt;

&lt;p&gt;Now consider another project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/

├── src/

│   ├── app.js

│   └── utils/

│       └── math.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside &lt;code&gt;app.js&lt;/code&gt;:&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;./utils/math&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 interprets this as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Current Folder

↓

utils

↓

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

&lt;/div&gt;



&lt;p&gt;Everything is relative to the file that is currently executing.&lt;/p&gt;




&lt;h1&gt;
  
  
  Going One Level Up
&lt;/h1&gt;

&lt;p&gt;Suppose you are inside:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/routes/app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and want to access:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/utils/math.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You write:&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;../utils/math&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;Here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;..
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Parent Directory&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Internally Node performs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;routes

↓

Go Up

↓

src

↓

utils

↓

math.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Going Multiple Levels Up
&lt;/h1&gt;

&lt;p&gt;You can continue moving upward.&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;../../config/database&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;means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Current Folder

↓

Parent

↓

Parent

↓

config

↓

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

&lt;/div&gt;



&lt;p&gt;This is exactly how your operating system navigates directories.&lt;/p&gt;

&lt;p&gt;Node simply follows the filesystem hierarchy.&lt;/p&gt;




&lt;h1&gt;
  
  
  Relative Paths vs Absolute Paths
&lt;/h1&gt;

&lt;p&gt;There are two ways to locate files.&lt;/p&gt;

&lt;h3&gt;
  
  
  Relative Path
&lt;/h3&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;./utils/math&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;Depends on the current module's location.&lt;/p&gt;




&lt;h3&gt;
  
  
  Absolute Path
&lt;/h3&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;C:/Projects/Bank/src/utils/math&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;or on Linux:&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;/home/user/project/src/utils/math&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;This specifies the complete location.&lt;/p&gt;

&lt;p&gt;Although Node supports absolute paths, they are rarely used in production because they make applications difficult to move between systems.&lt;/p&gt;

&lt;p&gt;Relative paths keep projects portable and maintainable.&lt;/p&gt;




&lt;h1&gt;
  
  
  How Does Node Decide?
&lt;/h1&gt;

&lt;p&gt;At this point, Node has learned one important thing.&lt;/p&gt;

&lt;p&gt;It asks a very simple question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Does the string start with &lt;code&gt;./&lt;/code&gt;, &lt;code&gt;../&lt;/code&gt;, or &lt;code&gt;/&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If YES,&lt;/p&gt;

&lt;p&gt;it is treated as a file path.&lt;/p&gt;

&lt;p&gt;If NO,&lt;/p&gt;

&lt;p&gt;Node first checks whether it is a Core Module.&lt;/p&gt;

&lt;p&gt;Only if it is not a Core Module does Node continue searching elsewhere.&lt;/p&gt;

&lt;p&gt;This tiny decision is the very first branch in the Module Resolution Algorithm.&lt;/p&gt;

&lt;p&gt;It determines the entire loading strategy.&lt;/p&gt;




&lt;h1&gt;
  
  
  Key Takeaways
&lt;/h1&gt;

&lt;p&gt;After reading this chapter, you should understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Module Resolution converts the argument passed to &lt;code&gt;require()&lt;/code&gt; into an actual file.&lt;/li&gt;
&lt;li&gt;Node classifies modules into Core, Local, and Third-Party modules.&lt;/li&gt;
&lt;li&gt;Core Modules are bundled with Node.js and are loaded immediately.&lt;/li&gt;
&lt;li&gt;Local Modules begin with &lt;code&gt;./&lt;/code&gt;, &lt;code&gt;../&lt;/code&gt;, or &lt;code&gt;/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;./&lt;/code&gt; means the current directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;..&lt;/code&gt; means the parent directory.&lt;/li&gt;
&lt;li&gt;Relative paths are preferred over absolute paths in production applications.&lt;/li&gt;
&lt;li&gt;The first step of the Module Resolution Algorithm is identifying what kind of module you are requesting.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Coming Next
&lt;/h1&gt;

&lt;p&gt;In &lt;strong&gt;Part 4.3A.2&lt;/strong&gt;, we'll continue the journey by answering questions that every backend developer eventually encounters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why does &lt;code&gt;require("./math")&lt;/code&gt; work without writing &lt;code&gt;.js&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;How does Node search for &lt;code&gt;.js&lt;/code&gt;, &lt;code&gt;.json&lt;/code&gt;, and &lt;code&gt;.node&lt;/code&gt; files?&lt;/li&gt;
&lt;li&gt;What happens when you require a folder instead of a file?&lt;/li&gt;
&lt;li&gt;Why does &lt;code&gt;index.js&lt;/code&gt; load automatically?&lt;/li&gt;
&lt;li&gt;How does &lt;code&gt;package.json&lt;/code&gt; influence Module Resolution?&lt;/li&gt;
&lt;li&gt;What is the exact resolution order followed internally by Node.js?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the end of Part &lt;strong&gt;4.3A.2&lt;/strong&gt;, you'll understand the complete algorithm Node.js uses before a module is ever executed.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>javascript</category>
      <category>node</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>From JavaScript to Node.js: Understanding What Really Happens Behind the Scenes (Part 4.2)</title>
      <dc:creator>Sudhanshu code</dc:creator>
      <pubDate>Fri, 24 Jul 2026 12:08:30 +0000</pubDate>
      <link>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-42-4nce</link>
      <guid>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-42-4nce</guid>
      <description>&lt;p&gt;The Hidden World of require(): Module Wrapper Function, module.exports, and exports&lt;/p&gt;

&lt;p&gt;In the previous article, we learned why modules exist.&lt;/p&gt;

&lt;p&gt;We discovered that every JavaScript file in Node.js is treated as an independent module with its own private scope.&lt;/p&gt;

&lt;p&gt;But a much bigger question remains.&lt;/p&gt;

&lt;p&gt;When we write:&lt;/p&gt;

&lt;p&gt;const math = require("./math");&lt;/p&gt;

&lt;p&gt;what actually happens?&lt;/p&gt;

&lt;p&gt;Does JavaScript understand require()?&lt;/p&gt;

&lt;p&gt;Does the V8 Engine load another file?&lt;/p&gt;

&lt;p&gt;Or does Node.js perform some hidden magic?&lt;/p&gt;

&lt;p&gt;Let's open the black box.&lt;/p&gt;

&lt;p&gt;Is require() a JavaScript Feature?&lt;/p&gt;

&lt;p&gt;One of the biggest misconceptions among beginners is this:&lt;/p&gt;

&lt;p&gt;"require() is part of JavaScript."&lt;/p&gt;

&lt;p&gt;It isn't.&lt;/p&gt;

&lt;p&gt;Open your browser console and type:&lt;/p&gt;

&lt;p&gt;require("./math");&lt;/p&gt;

&lt;p&gt;You'll get something similar to:&lt;/p&gt;

&lt;p&gt;ReferenceError: require is not defined&lt;/p&gt;

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

&lt;p&gt;Because JavaScript itself has no built-in function called require().&lt;/p&gt;

&lt;p&gt;The ECMAScript specification—the official definition of JavaScript—doesn't include it.&lt;/p&gt;

&lt;p&gt;So where does it come from?&lt;/p&gt;

&lt;p&gt;The answer is simple:&lt;/p&gt;

&lt;p&gt;require() is created by Node.js, not by JavaScript.&lt;/p&gt;

&lt;p&gt;It is part of the Node.js runtime.&lt;/p&gt;

&lt;p&gt;Then How Can We Use It in Every File?&lt;/p&gt;

&lt;p&gt;This is where Node.js does something brilliant.&lt;/p&gt;

&lt;p&gt;Suppose you create a file called:&lt;/p&gt;

&lt;p&gt;math.js&lt;/p&gt;

&lt;p&gt;containing:&lt;/p&gt;

&lt;p&gt;const PI = 3.14159;&lt;/p&gt;

&lt;p&gt;function add(a, b) {&lt;br&gt;
    return a + b;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;You wrote only these few lines.&lt;/p&gt;

&lt;p&gt;But Node does not execute this file exactly as it appears.&lt;/p&gt;

&lt;p&gt;Before execution, Node wraps your code inside another function.&lt;/p&gt;

&lt;p&gt;Conceptually, it becomes:&lt;/p&gt;

&lt;p&gt;(function (exports, require, module, __filename, __dirname) {&lt;/p&gt;

&lt;p&gt;const PI = 3.14159;&lt;/p&gt;

&lt;p&gt;function add(a, b) {&lt;br&gt;
    return a + b;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;This hidden function is called the Module Wrapper Function.&lt;/p&gt;

&lt;p&gt;Node creates it automatically for every CommonJS module.&lt;/p&gt;

&lt;p&gt;You never write it yourself.&lt;/p&gt;

&lt;p&gt;Why Wrap Every Module?&lt;/p&gt;

&lt;p&gt;At first, wrapping every file may seem unnecessary.&lt;/p&gt;

&lt;p&gt;But this single design solves multiple problems.&lt;/p&gt;

&lt;p&gt;It creates:&lt;/p&gt;

&lt;p&gt;A private scope&lt;br&gt;
Module isolation&lt;br&gt;
Access to require&lt;br&gt;
Access to module&lt;br&gt;
Access to exports&lt;br&gt;
Access to __filename&lt;br&gt;
Access to __dirname&lt;/p&gt;

&lt;p&gt;Without this wrapper, every variable would become global.&lt;/p&gt;

&lt;p&gt;Imagine two files.&lt;/p&gt;

&lt;p&gt;const PORT = 3000;&lt;/p&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;p&gt;const PORT = 5000;&lt;/p&gt;

&lt;p&gt;Without isolation, these variables would collide.&lt;/p&gt;

&lt;p&gt;Because of the wrapper, each file gets its own execution environment.&lt;/p&gt;

&lt;p&gt;Understanding the Wrapper Parameters&lt;/p&gt;

&lt;p&gt;Let's examine the hidden function carefully.&lt;/p&gt;

&lt;p&gt;(function (&lt;br&gt;
    exports,&lt;br&gt;
    require,&lt;br&gt;
    module,&lt;br&gt;
    __filename,&lt;br&gt;
    __dirname&lt;br&gt;
) {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Your code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;Every parameter has a purpose.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;require
const fs = require("fs");&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;require() loads another module.&lt;/p&gt;

&lt;p&gt;Node creates this function before your code starts executing.&lt;/p&gt;

&lt;p&gt;It is not provided by V8.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;module&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every file gets a module object.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;/p&gt;

&lt;p&gt;module = {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports: {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Initially,&lt;/p&gt;

&lt;p&gt;module.exports&lt;/p&gt;

&lt;p&gt;is simply an empty object.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;exports&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Another interesting parameter appears.&lt;/p&gt;

&lt;p&gt;exports&lt;/p&gt;

&lt;p&gt;Many beginners think&lt;/p&gt;

&lt;p&gt;exports&lt;/p&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;p&gt;module.exports&lt;/p&gt;

&lt;p&gt;are different objects.&lt;/p&gt;

&lt;p&gt;Initially...&lt;/p&gt;

&lt;p&gt;they are references to the same object.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;/p&gt;

&lt;p&gt;exports&lt;br&gt;
        │&lt;br&gt;
        ▼&lt;br&gt;
{}&lt;/p&gt;

&lt;p&gt;▲&lt;br&gt;
│&lt;/p&gt;

&lt;p&gt;module.exports&lt;/p&gt;

&lt;p&gt;Both variables point to the same object.&lt;/p&gt;

&lt;p&gt;Example&lt;br&gt;
exports.name = "Sudhanshu";&lt;/p&gt;

&lt;p&gt;Internally this becomes&lt;/p&gt;

&lt;p&gt;module.exports.name = "Sudhanshu";&lt;/p&gt;

&lt;p&gt;because both references currently point to the same object.&lt;/p&gt;

&lt;p&gt;When Do They Become Different?&lt;/p&gt;

&lt;p&gt;Suppose you write&lt;/p&gt;

&lt;p&gt;exports = {};&lt;/p&gt;

&lt;p&gt;Now&lt;/p&gt;

&lt;p&gt;exports&lt;/p&gt;

&lt;p&gt;points somewhere else.&lt;/p&gt;

&lt;p&gt;But&lt;/p&gt;

&lt;p&gt;module.exports&lt;/p&gt;

&lt;p&gt;still points to the original object.&lt;/p&gt;

&lt;p&gt;Visualization:&lt;/p&gt;

&lt;p&gt;Initially&lt;/p&gt;

&lt;p&gt;exports&lt;br&gt;
        │&lt;br&gt;
        ▼&lt;br&gt;
{}&lt;/p&gt;

&lt;p&gt;▲&lt;br&gt;
│&lt;/p&gt;

&lt;p&gt;module.exports&lt;/p&gt;

&lt;p&gt;After reassignment&lt;/p&gt;

&lt;p&gt;exports&lt;br&gt;
      │&lt;br&gt;
      ▼&lt;br&gt;
{}&lt;/p&gt;

&lt;p&gt;module.exports&lt;br&gt;
      │&lt;br&gt;
      ▼&lt;br&gt;
{ name: "Sudhanshu" }&lt;/p&gt;

&lt;p&gt;They are no longer connected.&lt;/p&gt;

&lt;p&gt;This is why Node returns module.exports, not exports.&lt;/p&gt;

&lt;p&gt;We'll revisit this in even more detail later.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;__filename&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Node automatically provides the absolute path of the current module.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;console.log(__filename);&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;C:\Projects\BankApp\math.js&lt;/p&gt;

&lt;p&gt;Useful for logging and locating resources.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;__dirname&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Similarly,&lt;/p&gt;

&lt;p&gt;console.log(__dirname);&lt;/p&gt;

&lt;p&gt;might produce&lt;/p&gt;

&lt;p&gt;C:\Projects\BankApp&lt;/p&gt;

&lt;p&gt;This is extremely useful when loading files relative to the current module.&lt;/p&gt;

&lt;p&gt;What Happens When We Call require()?&lt;/p&gt;

&lt;p&gt;Suppose our project looks like this.&lt;/p&gt;

&lt;p&gt;project/&lt;/p&gt;

&lt;p&gt;├── app.js&lt;/p&gt;

&lt;p&gt;└── math.js&lt;/p&gt;

&lt;p&gt;math.js&lt;/p&gt;

&lt;p&gt;function add(a, b) {&lt;br&gt;
    return a + b;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;module.exports = add;&lt;/p&gt;

&lt;p&gt;app.js&lt;/p&gt;

&lt;p&gt;const add = require("./math");&lt;/p&gt;

&lt;p&gt;console.log(add(2,3));&lt;/p&gt;

&lt;p&gt;Now let's follow the execution.&lt;/p&gt;

&lt;p&gt;Step 1&lt;/p&gt;

&lt;p&gt;Node begins executing&lt;/p&gt;

&lt;p&gt;app.js&lt;/p&gt;

&lt;p&gt;inside its own wrapper function.&lt;/p&gt;

&lt;p&gt;Step 2&lt;/p&gt;

&lt;p&gt;Execution reaches&lt;/p&gt;

&lt;p&gt;require("./math")&lt;/p&gt;

&lt;p&gt;Node recognizes that another module is required.&lt;/p&gt;

&lt;p&gt;Step 3&lt;/p&gt;

&lt;p&gt;Node resolves the path.&lt;/p&gt;

&lt;p&gt;It asks:&lt;/p&gt;

&lt;p&gt;Where is "./math"?&lt;/p&gt;

&lt;p&gt;Node tries to locate the corresponding file.&lt;/p&gt;

&lt;p&gt;Eventually it finds&lt;/p&gt;

&lt;p&gt;math.js&lt;br&gt;
Step 4&lt;/p&gt;

&lt;p&gt;Node reads the contents of the file.&lt;/p&gt;

&lt;p&gt;At this point,&lt;/p&gt;

&lt;p&gt;it has only plain text.&lt;/p&gt;

&lt;p&gt;function add(a,b){&lt;br&gt;
    return a+b;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;module.exports = add;&lt;/p&gt;

&lt;p&gt;Nothing has executed yet.&lt;/p&gt;

&lt;p&gt;Step 5&lt;/p&gt;

&lt;p&gt;Before execution,&lt;/p&gt;

&lt;p&gt;Node wraps the file.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;/p&gt;

&lt;p&gt;(function(exports, require, module, __filename, __dirname){&lt;/p&gt;

&lt;p&gt;function add(a,b){&lt;br&gt;
    return a+b;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;module.exports = add;&lt;/p&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;Notice that the wrapper is created before V8 executes the module.&lt;/p&gt;

&lt;p&gt;Step 6&lt;/p&gt;

&lt;p&gt;Now V8 executes the wrapped function.&lt;/p&gt;

&lt;p&gt;During execution,&lt;/p&gt;

&lt;p&gt;Node updates&lt;/p&gt;

&lt;p&gt;module.exports&lt;/p&gt;

&lt;p&gt;to reference&lt;/p&gt;

&lt;p&gt;add&lt;/p&gt;

&lt;p&gt;At the end of execution,&lt;/p&gt;

&lt;p&gt;the module object looks conceptually like:&lt;/p&gt;

&lt;p&gt;module = {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports: add
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
Step 7&lt;/p&gt;

&lt;p&gt;require() returns&lt;/p&gt;

&lt;p&gt;module.exports&lt;/p&gt;

&lt;p&gt;Therefore&lt;/p&gt;

&lt;p&gt;const add = require("./math");&lt;/p&gt;

&lt;p&gt;becomes conceptually equivalent to&lt;/p&gt;

&lt;p&gt;const add = module.exports;&lt;/p&gt;

&lt;p&gt;Now&lt;/p&gt;

&lt;p&gt;add(2,3)&lt;/p&gt;

&lt;p&gt;works exactly as expected.&lt;/p&gt;

&lt;p&gt;The Complete Flow&lt;/p&gt;

&lt;p&gt;Let's summarize everything.&lt;/p&gt;

&lt;p&gt;app.js&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;require("./math")&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Resolve File Path&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Read File&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Wrap Inside Module Function&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Create module object&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Create exports reference&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Provide require()&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Provide __filename&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Provide __dirname&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Execute with V8&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Populate module.exports&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Return module.exports&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Continue app.js&lt;/p&gt;

&lt;p&gt;This entire process happens every time a new CommonJS module is loaded.&lt;/p&gt;

&lt;p&gt;Why Doesn't V8 Do This?&lt;/p&gt;

&lt;p&gt;Because none of this belongs to JavaScript.&lt;/p&gt;

&lt;p&gt;V8 understands:&lt;/p&gt;

&lt;p&gt;Variables&lt;br&gt;
Functions&lt;br&gt;
Arrays&lt;br&gt;
Objects&lt;br&gt;
Loops&lt;br&gt;
Classes&lt;/p&gt;

&lt;p&gt;It does not understand:&lt;/p&gt;

&lt;p&gt;Files&lt;br&gt;
Modules&lt;br&gt;
require()&lt;br&gt;
module.exports&lt;br&gt;
__dirname&lt;/p&gt;

&lt;p&gt;Those are runtime features provided by Node.js.&lt;/p&gt;

&lt;p&gt;This distinction is extremely important.&lt;/p&gt;

&lt;p&gt;Key Takeaways&lt;/p&gt;

&lt;p&gt;After reading this article, you should understand:&lt;/p&gt;

&lt;p&gt;require() is not part of JavaScript.&lt;br&gt;
Node.js injects require() into every CommonJS module.&lt;br&gt;
Every module is wrapped inside a hidden function before execution.&lt;br&gt;
The wrapper creates a private scope.&lt;br&gt;
module.exports is the object that Node actually returns.&lt;br&gt;
exports is initially just another reference to module.exports.&lt;br&gt;
Reassigning exports does not change module.exports.&lt;br&gt;
V8 executes the wrapped function, but the wrapper itself is created by Node.js.&lt;br&gt;
Coming Next&lt;/p&gt;

&lt;p&gt;In Part 4.3, we'll go even deeper.&lt;/p&gt;

&lt;p&gt;We'll answer questions such as:&lt;/p&gt;

&lt;p&gt;How does Node know where to find a module?&lt;br&gt;
Why does require("./math") work without writing .js?&lt;br&gt;
How does Node search node_modules?&lt;br&gt;
What is the Module Resolution Algorithm?&lt;br&gt;
Why is require() synchronous?&lt;br&gt;
What is Module Cache, and why is a module executed only once?&lt;br&gt;
How do Circular Dependencies work internally?&lt;/p&gt;

&lt;p&gt;By the end of Part 4.3, you'll understand the entire lifecycle of a CommonJS module—from the moment you type require() until Node returns the exported value.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>javascript</category>
      <category>node</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>From JavaScript to Node.js: Understanding What Really Happens Behind the Scenes (Part 4.1)</title>
      <dc:creator>Sudhanshu code</dc:creator>
      <pubDate>Fri, 24 Jul 2026 12:07:15 +0000</pubDate>
      <link>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-41-3ohl</link>
      <guid>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-41-3ohl</guid>
      <description>&lt;p&gt;Node.js Modules Explained from Scratch — Why Do Modules Even Exist?&lt;/p&gt;

&lt;p&gt;If you've been writing Node.js for even a few days, you've probably seen code like this:&lt;/p&gt;

&lt;p&gt;const fs = require("fs");&lt;br&gt;
const express = require("express");&lt;br&gt;
const path = require("path");&lt;/p&gt;

&lt;p&gt;Or maybe you've written your own module:&lt;/p&gt;

&lt;p&gt;const math = require("./math");&lt;/p&gt;

&lt;p&gt;Most tutorials stop here.&lt;/p&gt;

&lt;p&gt;They teach how to use require(), but very few explain why modules exist in the first place.&lt;/p&gt;

&lt;p&gt;In this article, we're not going to start with require().&lt;/p&gt;

&lt;p&gt;Instead, we're going to start with a much more fundamental question.&lt;/p&gt;

&lt;p&gt;Why were modules invented at all?&lt;/p&gt;

&lt;p&gt;Because once you understand that, everything else in Node.js modules starts making sense.&lt;/p&gt;

&lt;p&gt;Imagine Building Everything in One File&lt;/p&gt;

&lt;p&gt;Let's say you're creating a banking application.&lt;/p&gt;

&lt;p&gt;At first, everything feels simple.&lt;/p&gt;

&lt;p&gt;function login() {}&lt;/p&gt;

&lt;p&gt;function logout() {}&lt;/p&gt;

&lt;p&gt;function transferMoney() {}&lt;/p&gt;

&lt;p&gt;function checkBalance() {}&lt;/p&gt;

&lt;p&gt;function createAccount() {}&lt;/p&gt;

&lt;p&gt;function updateProfile() {}&lt;/p&gt;

&lt;p&gt;function deleteAccount() {}&lt;/p&gt;

&lt;p&gt;function sendOTP() {}&lt;/p&gt;

&lt;p&gt;function generateToken() {}&lt;/p&gt;

&lt;p&gt;function connectDatabase() {}&lt;/p&gt;

&lt;p&gt;Looks manageable.&lt;/p&gt;

&lt;p&gt;Now imagine your application grows.&lt;/p&gt;

&lt;p&gt;After six months, your file contains:&lt;/p&gt;

&lt;p&gt;Authentication&lt;br&gt;
Database Logic&lt;br&gt;
Payment Logic&lt;br&gt;
Email Service&lt;br&gt;
Notifications&lt;br&gt;
User Management&lt;br&gt;
Admin Dashboard&lt;br&gt;
Reports&lt;br&gt;
Logging&lt;br&gt;
Validation&lt;br&gt;
Utility Functions&lt;/p&gt;

&lt;p&gt;One file becomes&lt;/p&gt;

&lt;p&gt;app.js&lt;/p&gt;

&lt;p&gt;25,000 lines&lt;/p&gt;

&lt;p&gt;Can one developer understand it easily?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Can ten developers work on the same file?&lt;/p&gt;

&lt;p&gt;Also no.&lt;/p&gt;

&lt;p&gt;Can bugs be found quickly?&lt;/p&gt;

&lt;p&gt;Again, no.&lt;/p&gt;

&lt;p&gt;So the problem wasn't JavaScript.&lt;/p&gt;

&lt;p&gt;The problem was organization.&lt;/p&gt;

&lt;p&gt;Software Is Built by Teams&lt;/p&gt;

&lt;p&gt;Modern software isn't written by one person.&lt;/p&gt;

&lt;p&gt;Imagine a company with 40 backend engineers.&lt;/p&gt;

&lt;p&gt;If everyone edits the same file...&lt;/p&gt;

&lt;p&gt;app.js&lt;/p&gt;

&lt;p&gt;chaos begins.&lt;/p&gt;

&lt;p&gt;Developer A changes authentication.&lt;/p&gt;

&lt;p&gt;Developer B edits payments.&lt;/p&gt;

&lt;p&gt;Developer C modifies notifications.&lt;/p&gt;

&lt;p&gt;Developer D updates database code.&lt;/p&gt;

&lt;p&gt;Soon everyone is editing the same file.&lt;/p&gt;

&lt;p&gt;Conflicts become unavoidable.&lt;/p&gt;

&lt;p&gt;Software engineering needed a better way to organize code.&lt;/p&gt;

&lt;p&gt;That idea became modules.&lt;/p&gt;

&lt;p&gt;What Is a Module?&lt;/p&gt;

&lt;p&gt;The simplest definition is:&lt;/p&gt;

&lt;p&gt;A module is a self-contained piece of code responsible for one specific task.&lt;/p&gt;

&lt;p&gt;Instead of writing everything together...&lt;/p&gt;

&lt;p&gt;we divide the application into small independent files.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;project/&lt;/p&gt;

&lt;p&gt;├── app.js&lt;/p&gt;

&lt;p&gt;├── auth.js&lt;/p&gt;

&lt;p&gt;├── payment.js&lt;/p&gt;

&lt;p&gt;├── database.js&lt;/p&gt;

&lt;p&gt;├── email.js&lt;/p&gt;

&lt;p&gt;├── utils.js&lt;/p&gt;

&lt;p&gt;└── logger.js&lt;/p&gt;

&lt;p&gt;Every file has a single responsibility.&lt;/p&gt;

&lt;p&gt;This makes applications easier to understand, test, debug, and maintain.&lt;/p&gt;

&lt;p&gt;Modules Are Not a Node.js Feature&lt;/p&gt;

&lt;p&gt;This surprises many beginners.&lt;/p&gt;

&lt;p&gt;People often say:&lt;/p&gt;

&lt;p&gt;"Modules are a Node.js concept."&lt;/p&gt;

&lt;p&gt;Not exactly.&lt;/p&gt;

&lt;p&gt;The idea of modules is much older than Node.js.&lt;/p&gt;

&lt;p&gt;Many programming languages support modular programming.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;p&gt;Java Packages&lt;br&gt;
Python Modules&lt;br&gt;
C Libraries&lt;br&gt;
C++ Header Files&lt;br&gt;
Rust Crates&lt;br&gt;
Go Packages&lt;/p&gt;

&lt;p&gt;Node.js adopted this idea for JavaScript.&lt;/p&gt;

&lt;p&gt;JavaScript Before Modules&lt;/p&gt;

&lt;p&gt;When JavaScript was created in 1995...&lt;/p&gt;

&lt;p&gt;there were no modules.&lt;/p&gt;

&lt;p&gt;The language was designed to make web pages interactive.&lt;/p&gt;

&lt;p&gt;Most websites contained only a few lines of JavaScript.&lt;/p&gt;

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

&lt;p&gt;alert("Welcome");&lt;/p&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;p&gt;button.onclick = function () {&lt;br&gt;
    alert("Clicked");&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;Nobody imagined JavaScript would one day power:&lt;/p&gt;

&lt;p&gt;Netflix&lt;br&gt;
PayPal&lt;br&gt;
LinkedIn&lt;br&gt;
Uber&lt;br&gt;
VS Code&lt;br&gt;
Discord&lt;/p&gt;

&lt;p&gt;Because applications were tiny...&lt;/p&gt;

&lt;p&gt;modules weren't considered necessary.&lt;/p&gt;

&lt;p&gt;Then JavaScript Started Growing&lt;/p&gt;

&lt;p&gt;Around the mid-2000s, developers started building larger web applications.&lt;/p&gt;

&lt;p&gt;Instead of&lt;/p&gt;

&lt;p&gt;200 lines&lt;/p&gt;

&lt;p&gt;applications became&lt;/p&gt;

&lt;p&gt;20,000 lines&lt;/p&gt;

&lt;p&gt;50,000 lines&lt;/p&gt;

&lt;p&gt;100,000+ lines&lt;/p&gt;

&lt;p&gt;Developers desperately needed code organization.&lt;/p&gt;

&lt;p&gt;But JavaScript still had no official module system.&lt;/p&gt;

&lt;p&gt;People started inventing their own solutions.&lt;/p&gt;

&lt;p&gt;Before Node.js, Developers Used the Global Scope&lt;/p&gt;

&lt;p&gt;Suppose you have two files.&lt;/p&gt;

&lt;p&gt;math.js&lt;/p&gt;

&lt;p&gt;function add(a, b) {&lt;br&gt;
    return a + b;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;app.js&lt;/p&gt;

&lt;p&gt;console.log(add(2,3));&lt;/p&gt;

&lt;p&gt;Seems fine.&lt;/p&gt;

&lt;p&gt;Until another developer writes&lt;/p&gt;

&lt;p&gt;function add() {}&lt;/p&gt;

&lt;p&gt;in another file.&lt;/p&gt;

&lt;p&gt;Now both functions exist globally.&lt;/p&gt;

&lt;p&gt;Which one should JavaScript use?&lt;/p&gt;

&lt;p&gt;Nobody knows.&lt;/p&gt;

&lt;p&gt;This became known as Global Namespace Pollution.&lt;/p&gt;

&lt;p&gt;Everything lived in one shared global environment.&lt;/p&gt;

&lt;p&gt;Large applications became difficult to maintain.&lt;/p&gt;

&lt;p&gt;The Global Scope Problem&lt;/p&gt;

&lt;p&gt;Imagine one whiteboard shared by an entire company.&lt;/p&gt;

&lt;p&gt;Everyone writes on the same board.&lt;/p&gt;

&lt;p&gt;Soon:&lt;/p&gt;

&lt;p&gt;names collide&lt;br&gt;
variables overwrite each other&lt;br&gt;
debugging becomes painful&lt;br&gt;
maintenance becomes difficult&lt;/p&gt;

&lt;p&gt;The JavaScript global scope behaved similarly.&lt;/p&gt;

&lt;p&gt;Modules solved this problem.&lt;/p&gt;

&lt;p&gt;How Modules Solve the Problem&lt;/p&gt;

&lt;p&gt;Instead of one giant shared environment...&lt;/p&gt;

&lt;p&gt;every file receives its own private scope.&lt;/p&gt;

&lt;p&gt;Imagine:&lt;/p&gt;

&lt;p&gt;math.js&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Private Room&lt;br&gt;
auth.js&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Private Room&lt;br&gt;
database.js&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;Private Room&lt;/p&gt;

&lt;p&gt;Each file can safely define:&lt;/p&gt;

&lt;p&gt;const PORT = 3000;&lt;/p&gt;

&lt;p&gt;without affecting another file.&lt;/p&gt;

&lt;p&gt;This isolation is one of the biggest advantages of modules.&lt;/p&gt;

&lt;p&gt;Every File Is a Module in Node.js&lt;/p&gt;

&lt;p&gt;One of the most beautiful design decisions in Node.js is incredibly simple.&lt;/p&gt;

&lt;p&gt;Every JavaScript file is automatically treated as a separate module.&lt;/p&gt;

&lt;p&gt;Suppose we have:&lt;/p&gt;

&lt;p&gt;project/&lt;/p&gt;

&lt;p&gt;├── app.js&lt;/p&gt;

&lt;p&gt;└── math.js&lt;/p&gt;

&lt;p&gt;math.js&lt;/p&gt;

&lt;p&gt;const PI = 3.14159;&lt;/p&gt;

&lt;p&gt;function add(a, b) {&lt;br&gt;
    return a + b;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;At first glance, you might expect both PI and add() to be available everywhere.&lt;/p&gt;

&lt;p&gt;They are not.&lt;/p&gt;

&lt;p&gt;Open another file.&lt;/p&gt;

&lt;p&gt;app.js&lt;/p&gt;

&lt;p&gt;console.log(PI);&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;ReferenceError: PI is not defined&lt;/p&gt;

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

&lt;p&gt;Because every file has its own scope.&lt;/p&gt;

&lt;p&gt;This is one of Node.js's most important design decisions.&lt;/p&gt;

&lt;p&gt;Why Doesn't One File See Another File's Variables?&lt;/p&gt;

&lt;p&gt;Think about a house.&lt;/p&gt;

&lt;p&gt;Every room has its own door.&lt;/p&gt;

&lt;p&gt;Just because your bedroom contains a table...&lt;/p&gt;

&lt;p&gt;doesn't mean the kitchen automatically has access to it.&lt;/p&gt;

&lt;p&gt;Node.js modules work similarly.&lt;/p&gt;

&lt;p&gt;Each file is isolated.&lt;/p&gt;

&lt;p&gt;Nothing leaves that file unless you explicitly allow it.&lt;/p&gt;

&lt;p&gt;That is where module.exports enters the picture.&lt;/p&gt;

&lt;p&gt;We'll study that in the next article.&lt;/p&gt;

&lt;p&gt;How Does Node Know a File Is a Module?&lt;/p&gt;

&lt;p&gt;This is where things become interesting.&lt;/p&gt;

&lt;p&gt;When Node loads&lt;/p&gt;

&lt;p&gt;math.js&lt;/p&gt;

&lt;p&gt;it does not execute the file exactly as you wrote it.&lt;/p&gt;

&lt;p&gt;Internally...&lt;/p&gt;

&lt;p&gt;Node wraps your code before executing it.&lt;/p&gt;

&lt;p&gt;Something similar to this happens behind the scenes:&lt;/p&gt;

&lt;p&gt;(function (exports, require, module, __filename, __dirname) {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Your code lives here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;This is called the Module Wrapper Function.&lt;/p&gt;

&lt;p&gt;We'll explore it deeply in Part 4.2.&lt;/p&gt;

&lt;p&gt;For now, remember one important idea:&lt;/p&gt;

&lt;p&gt;Every file receives its own private execution environment.&lt;/p&gt;

&lt;p&gt;That single design decision solves a huge number of software engineering problems.&lt;/p&gt;

&lt;p&gt;Benefits of Modules&lt;/p&gt;

&lt;p&gt;Modules provide much more than cleaner code.&lt;/p&gt;

&lt;p&gt;They enable:&lt;/p&gt;

&lt;p&gt;Better code organization&lt;br&gt;
Reusability&lt;br&gt;
Encapsulation&lt;br&gt;
Easier testing&lt;br&gt;
Team collaboration&lt;br&gt;
Maintainability&lt;br&gt;
Separation of concerns&lt;br&gt;
Reduced naming conflicts&lt;br&gt;
Improved scalability&lt;/p&gt;

&lt;p&gt;These principles apply across almost every modern programming language.&lt;/p&gt;

&lt;p&gt;Key Takeaways&lt;/p&gt;

&lt;p&gt;After reading this article, you should understand that:&lt;/p&gt;

&lt;p&gt;Modules were created to solve software organization problems.&lt;br&gt;
JavaScript originally had no module system.&lt;br&gt;
Large applications exposed the limitations of the global scope.&lt;br&gt;
Node.js treats every JavaScript file as a separate module.&lt;br&gt;
Every module has its own private scope.&lt;br&gt;
Variables inside one file are not automatically visible in another.&lt;br&gt;
Node internally wraps every module before executing it.&lt;br&gt;
This wrapper creates isolation and lays the foundation for require() and module.exports.&lt;br&gt;
Coming Next&lt;/p&gt;

&lt;p&gt;In Part 4.2, we'll open the black box and answer questions that almost every Node.js developer asks:&lt;/p&gt;

&lt;p&gt;What actually happens when you write require("./math")?&lt;br&gt;
Is require() part of JavaScript or Node.js?&lt;br&gt;
What is the Module Wrapper Function?&lt;br&gt;
Why do exports and module.exports exist?&lt;br&gt;
How does one module communicate with another?&lt;br&gt;
What does Node execute behind the scenes before your code runs?&lt;/p&gt;

&lt;p&gt;We'll move from using modules...&lt;/p&gt;

&lt;p&gt;to understanding how Node.js builds its entire module system internally.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>node</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>From JavaScript to Node.js: Understanding What Really Happens Behind the Scenes (Part 3)</title>
      <dc:creator>Sudhanshu code</dc:creator>
      <pubDate>Fri, 24 Jul 2026 12:05:20 +0000</pubDate>
      <link>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-3-34gj</link>
      <guid>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-3-34gj</guid>
      <description>&lt;h1&gt;
  
  
  From JavaScript to Node.js: Understanding What Really Happens Behind the Scenes (Part 3)
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Synchronous vs Asynchronous Node.js: What Really Happens Inside &lt;code&gt;fs.readFile()&lt;/code&gt;?
&lt;/h1&gt;

&lt;p&gt;In the previous article, we learned that Node.js is much more than the V8 engine.&lt;/p&gt;

&lt;p&gt;We discovered that a simple file operation travels through multiple layers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript
    ↓
V8 Engine
    ↓
Node APIs
    ↓
C++ Bindings
    ↓
libuv
    ↓
Operating System
    ↓
Hardware
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But one important question still remains unanswered.&lt;/p&gt;

&lt;p&gt;If reading a file requires communicating with the operating system and the storage device...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why doesn't Node.js freeze while waiting for the file?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To answer that, we first need to understand one of the most important ideas in computer science.&lt;/p&gt;




&lt;h1&gt;
  
  
  What Does "Blocking" Actually Mean?
&lt;/h1&gt;

&lt;p&gt;Most tutorials define blocking like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Blocking means the program waits."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That definition is correct...&lt;/p&gt;

&lt;p&gt;but it doesn't explain &lt;strong&gt;what is actually waiting&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's look deeper.&lt;/p&gt;

&lt;p&gt;Consider this program.&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="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;Start&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;fs&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;fs&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message.txt&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;utf8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&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;End&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;Many developers say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Node waits."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But technically, that's not what happens.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;current JavaScript execution cannot continue because the current function has not finished.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The JavaScript thread is occupied.&lt;/p&gt;

&lt;p&gt;Imagine the execution like a single employee working at a desk.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Current Task

↓

Read File

↓

Not Finished Yet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Until that task finishes...&lt;/p&gt;

&lt;p&gt;the employee cannot start another task.&lt;/p&gt;

&lt;p&gt;This is called &lt;strong&gt;blocking&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Understanding the Call Stack
&lt;/h1&gt;

&lt;p&gt;Before discussing asynchronous programming, we need to understand the Call Stack.&lt;/p&gt;

&lt;p&gt;The Call Stack is a data structure that keeps track of which JavaScript function is currently executing.&lt;/p&gt;

&lt;p&gt;Imagine it as a stack of books.&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="nx"&gt;Top&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="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nx"&gt;Bottom&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever a function starts, it is pushed onto the stack.&lt;/p&gt;

&lt;p&gt;When it finishes, it is removed.&lt;/p&gt;

&lt;p&gt;Only the function at the top of the stack can execute.&lt;/p&gt;

&lt;p&gt;JavaScript executes &lt;strong&gt;one stack frame at a time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is one of the reasons JavaScript is called &lt;strong&gt;single-threaded&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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;Hello&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="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;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&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;End&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;Call Stack visualization&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="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;Start&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="err"&gt;↓&lt;/span&gt;

&lt;span class="nx"&gt;Removed&lt;/span&gt;

&lt;span class="err"&gt;↓&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="err"&gt;↓&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;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="err"&gt;↓&lt;/span&gt;

&lt;span class="nx"&gt;Removed&lt;/span&gt;

&lt;span class="err"&gt;↓&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;End&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;Only one frame executes at a time.&lt;/p&gt;

&lt;p&gt;There is no parallel execution happening here.&lt;/p&gt;




&lt;h1&gt;
  
  
  What Happens Inside &lt;code&gt;readFileSync()&lt;/code&gt;?
&lt;/h1&gt;

&lt;p&gt;Let's follow the execution carefully.&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;fs&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;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&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;1&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;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;notes.txt&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;utf8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&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="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&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;2&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;Execution begins.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;console.log("1")

↓

Output

1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next comes&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node sends the request down the runtime layers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript

↓

Node API

↓

C++ Binding

↓

Operating System

↓

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

&lt;/div&gt;



&lt;p&gt;At this moment...&lt;/p&gt;

&lt;p&gt;JavaScript cannot continue.&lt;/p&gt;

&lt;p&gt;The Call Stack still contains&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;readFileSync&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Until it returns,&lt;/p&gt;

&lt;p&gt;this line&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="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;2&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;cannot execute.&lt;/p&gt;

&lt;p&gt;The result arrives.&lt;/p&gt;

&lt;p&gt;The stack becomes free.&lt;/p&gt;

&lt;p&gt;Execution continues.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1

(File Content)

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

&lt;/div&gt;



&lt;p&gt;Nothing surprising happened.&lt;/p&gt;

&lt;p&gt;The execution simply stopped until the file arrived.&lt;/p&gt;

&lt;p&gt;This is synchronous execution.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Is It Called Synchronous?
&lt;/h1&gt;

&lt;p&gt;Because every operation happens &lt;strong&gt;in sequence&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;Task A

↓

Complete

↓

Task B

↓

Complete

↓

Task C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing jumps ahead.&lt;/p&gt;

&lt;p&gt;Nothing overlaps.&lt;/p&gt;

&lt;p&gt;Every step waits for the previous one.&lt;/p&gt;




&lt;h1&gt;
  
  
  Now Let's Look at &lt;code&gt;readFile()&lt;/code&gt;
&lt;/h1&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;fs&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;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&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;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;notes.txt&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;utf8&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="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&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="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&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;2&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;Many beginners imagine that JavaScript starts another thread.&lt;/p&gt;

&lt;p&gt;It doesn't.&lt;/p&gt;

&lt;p&gt;Let's see the real flow.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 1
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;1&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;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  Step 2
&lt;/h1&gt;

&lt;p&gt;JavaScript reaches&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node prepares a native request.&lt;/p&gt;

&lt;p&gt;The request moves through&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node API

↓

C++

↓

libuv

↓

Operating System
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something important.&lt;/p&gt;

&lt;p&gt;The file has &lt;strong&gt;not&lt;/strong&gt; been read yet.&lt;/p&gt;

&lt;p&gt;Only the request has been submitted.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 3
&lt;/h1&gt;

&lt;p&gt;At this point...&lt;/p&gt;

&lt;p&gt;JavaScript has nothing left to do with that request.&lt;/p&gt;

&lt;p&gt;The current function finishes immediately.&lt;/p&gt;

&lt;p&gt;The Call Stack becomes free.&lt;/p&gt;

&lt;p&gt;Now JavaScript executes&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="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;2&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;Output becomes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1

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

&lt;/div&gt;



&lt;p&gt;The file still hasn't arrived.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 4
&lt;/h1&gt;

&lt;p&gt;The operating system eventually finishes reading the file.&lt;/p&gt;

&lt;p&gt;The data returns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SSD

↓

Operating System

↓

libuv

↓

Node Runtime
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The callback is now ready.&lt;/p&gt;

&lt;p&gt;Later, when JavaScript is free to execute it, the callback runs and prints the file.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1

2

Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is why asynchronous code appears to "continue."&lt;/p&gt;

&lt;p&gt;JavaScript didn't keep reading the file.&lt;/p&gt;

&lt;p&gt;It simply delegated that responsibility.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Biggest Misconception
&lt;/h1&gt;

&lt;p&gt;People often say&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Node.js performs asynchronous operations."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That sentence is incomplete.&lt;/p&gt;

&lt;p&gt;A better statement is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Node.js delegates long-running operations to the operating system (through libuv and native bindings), allowing JavaScript to continue executing other work while the result is being prepared.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is the real reason asynchronous programming exists.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Doesn't V8 Read the File Directly?
&lt;/h1&gt;

&lt;p&gt;Because V8 was never designed to understand operating systems.&lt;/p&gt;

&lt;p&gt;V8 understands JavaScript.&lt;/p&gt;

&lt;p&gt;It understands things like&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; understand&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NTFS&lt;/li&gt;
&lt;li&gt;ext4&lt;/li&gt;
&lt;li&gt;APFS&lt;/li&gt;
&lt;li&gt;TCP sockets&lt;/li&gt;
&lt;li&gt;DNS&lt;/li&gt;
&lt;li&gt;File permissions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those belong to the operating system.&lt;/p&gt;

&lt;p&gt;Node bridges that gap.&lt;/p&gt;




&lt;h1&gt;
  
  
  Does libuv Read the File?
&lt;/h1&gt;

&lt;p&gt;Another common misconception.&lt;/p&gt;

&lt;p&gt;Many developers think&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"libuv reads the file."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not exactly.&lt;/p&gt;

&lt;p&gt;In most file operations, the operating system ultimately performs the actual file read.&lt;/p&gt;

&lt;p&gt;libuv coordinates the asynchronous workflow, abstracts platform differences, and integrates the result back into Node.js in a consistent way across operating systems.&lt;/p&gt;

&lt;p&gt;Think of libuv as an expert traffic controller—not the storage device itself.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Is Node.js Fast?
&lt;/h1&gt;

&lt;p&gt;Many articles say&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Node.js is fast because it is asynchronous."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's only part of the story.&lt;/p&gt;

&lt;p&gt;Node.js is fast because it avoids wasting the JavaScript thread waiting for slow I/O operations.&lt;/p&gt;

&lt;p&gt;Imagine a restaurant.&lt;/p&gt;

&lt;p&gt;One waiter.&lt;/p&gt;

&lt;p&gt;One customer asks for coffee.&lt;/p&gt;

&lt;p&gt;Brewing coffee takes five minutes.&lt;/p&gt;

&lt;p&gt;A poor workflow would have the waiter stand beside the coffee machine doing nothing.&lt;/p&gt;

&lt;p&gt;A better workflow is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take the order.&lt;/li&gt;
&lt;li&gt;Give it to the kitchen.&lt;/li&gt;
&lt;li&gt;Serve other customers.&lt;/li&gt;
&lt;li&gt;Return when the coffee is ready.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Node.js follows the second model.&lt;/p&gt;

&lt;p&gt;The JavaScript thread stays productive.&lt;/p&gt;




&lt;h1&gt;
  
  
  Synchronous vs Asynchronous
&lt;/h1&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Synchronous&lt;/th&gt;
&lt;th&gt;Asynchronous&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Blocking&lt;/td&gt;
&lt;td&gt;Non-blocking (for the JavaScript thread)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Waits for completion&lt;/td&gt;
&lt;td&gt;Delegates work and continues&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Simpler control flow&lt;/td&gt;
&lt;td&gt;Better scalability for I/O&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can freeze execution&lt;/td&gt;
&lt;td&gt;Keeps the thread available for other work&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Neither is "better" in every situation.&lt;/p&gt;

&lt;p&gt;Sometimes synchronous code is exactly what you need—especially during startup scripts, configuration loading, or command-line tools.&lt;/p&gt;

&lt;p&gt;Engineering is about choosing the right tool.&lt;/p&gt;




&lt;h1&gt;
  
  
  Key Takeaways
&lt;/h1&gt;

&lt;p&gt;After reading this article, you should be able to explain these ideas confidently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript executes on a single main thread.&lt;/li&gt;
&lt;li&gt;The Call Stack allows only one JavaScript function to execute at a time.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;readFileSync()&lt;/code&gt; blocks because the current execution cannot continue until it returns.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;readFile()&lt;/code&gt; submits work to the Node runtime and operating system, allowing JavaScript to continue.&lt;/li&gt;
&lt;li&gt;V8 executes JavaScript—it does not understand operating-system resources.&lt;/li&gt;
&lt;li&gt;Node.js extends JavaScript by providing native APIs and runtime infrastructure.&lt;/li&gt;
&lt;li&gt;libuv helps Node provide efficient, cross-platform asynchronous I/O.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are not just interview concepts.&lt;/p&gt;

&lt;p&gt;They are the foundation of every Express server, every database query, every API request, and every backend application built with Node.js.&lt;/p&gt;




&lt;h1&gt;
  
  
  Coming Next
&lt;/h1&gt;

&lt;p&gt;In Part 4, we'll explore one of the most misunderstood topics in Node.js:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Modules, &lt;code&gt;require()&lt;/code&gt;, &lt;code&gt;module.exports&lt;/code&gt;, &lt;code&gt;exports&lt;/code&gt;, Module Wrapper Function, and Module Cache&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We'll answer questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What actually happens when you write &lt;code&gt;require("fs")&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Is &lt;code&gt;require()&lt;/code&gt; a JavaScript feature or a Node.js feature?&lt;/li&gt;
&lt;li&gt;Why is every file treated as a separate module?&lt;/li&gt;
&lt;li&gt;How does Node avoid loading the same module twice?&lt;/li&gt;
&lt;li&gt;What is the Module Wrapper Function?&lt;/li&gt;
&lt;li&gt;Why do &lt;code&gt;exports&lt;/code&gt; and &lt;code&gt;module.exports&lt;/code&gt; sometimes behave differently?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the end of Part 4, you'll understand Node.js modules from the inside out—not just how to use them, but how they work.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>javascript</category>
      <category>node</category>
      <category>programming</category>
    </item>
    <item>
      <title>From JavaScript to Node.js: Understanding What Really Happens Behind the Scenes (Part 2)</title>
      <dc:creator>Sudhanshu code</dc:creator>
      <pubDate>Fri, 24 Jul 2026 12:04:40 +0000</pubDate>
      <link>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-2-35g4</link>
      <guid>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-2-35g4</guid>
      <description>&lt;h1&gt;
  
  
  From JavaScript to Node.js: Understanding What Really Happens Behind the Scenes (Part 2)
&lt;/h1&gt;

&lt;h1&gt;
  
  
  What Actually Happens After You Run &lt;code&gt;node app.js&lt;/code&gt;?
&lt;/h1&gt;

&lt;p&gt;In the previous article, we answered one important question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why does Node.js exist?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we're going to answer a much deeper question.&lt;/p&gt;

&lt;p&gt;What actually happens when you execute this command?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most developers know that "Node executes JavaScript."&lt;/p&gt;

&lt;p&gt;But that's only the surface.&lt;/p&gt;

&lt;p&gt;Behind this simple command, thousands of lines of C++, operating system calls, memory allocation, and multiple software layers start working together.&lt;/p&gt;

&lt;p&gt;Understanding this journey completely changed the way I think about backend development.&lt;/p&gt;

&lt;p&gt;Let's follow that journey.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 1 — Your JavaScript File Is Just Text
&lt;/h1&gt;

&lt;p&gt;Suppose your application looks like 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="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;Application Started&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;Before execution, &lt;strong&gt;app.js is nothing more than a text file stored on your SSD or HDD.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The computer does &lt;strong&gt;not&lt;/strong&gt; see JavaScript.&lt;/p&gt;

&lt;p&gt;It only sees bytes.&lt;/p&gt;

&lt;p&gt;Imagine your file as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Storage
│
├── app.js
│
└── Contains plain text characters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing has executed yet.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 2 — The Operating System Starts Node.js
&lt;/h1&gt;

&lt;p&gt;When you type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;your terminal doesn't magically understand JavaScript.&lt;/p&gt;

&lt;p&gt;Instead, your operating system does something similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Windows / Linux / macOS

↓

Find node executable

↓

Create a new process

↓

Allocate memory

↓

Load Node.js into RAM

↓

Start execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is extremely important.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The operating system starts Node.js first.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Node.js starts your JavaScript later.&lt;/p&gt;

&lt;p&gt;Many beginners think:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript

↓

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

&lt;/div&gt;



&lt;p&gt;Reality is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Operating System

↓

Node Process

↓

V8

↓

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

&lt;/div&gt;






&lt;h1&gt;
  
  
  What Is a Process?
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;process&lt;/strong&gt; is simply a running instance of a program.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Chrome.exe

↓

Running

↓

Chrome Process
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node app.js

↓

Running

↓

Node Process
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every process has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;its own memory&lt;/li&gt;
&lt;li&gt;its own call stack&lt;/li&gt;
&lt;li&gt;its own heap&lt;/li&gt;
&lt;li&gt;its own resources&lt;/li&gt;
&lt;li&gt;its own execution context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This isolation is one reason why applications don't accidentally overwrite each other's memory.&lt;/p&gt;




&lt;h1&gt;
  
  
  Step 3 — Node Initializes the Runtime
&lt;/h1&gt;

&lt;p&gt;After the process starts, Node initializes several internal systems.&lt;/p&gt;

&lt;p&gt;These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;V8 Engine&lt;/li&gt;
&lt;li&gt;Memory&lt;/li&gt;
&lt;li&gt;Event Loop&lt;/li&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;li&gt;Internal Modules&lt;/li&gt;
&lt;li&gt;libuv&lt;/li&gt;
&lt;li&gt;Environment Variables&lt;/li&gt;
&lt;li&gt;Process Object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Only after all of these are prepared does Node begin loading your JavaScript.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Biggest Misconception About Node.js
&lt;/h1&gt;

&lt;p&gt;Many developers imagine this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript

↓

Node.js

↓

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

&lt;/div&gt;



&lt;p&gt;This is incomplete.&lt;/p&gt;

&lt;p&gt;The real architecture is much larger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your JavaScript
        │
        ▼
      V8 Engine
        │
        ▼
 Node.js APIs
        │
        ▼
 C++ Bindings
        │
        ▼
      libuv
        │
        ▼
Operating System
        │
        ▼
Hardware
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every layer has a different responsibility.&lt;/p&gt;

&lt;p&gt;Let's understand every one.&lt;/p&gt;




&lt;h1&gt;
  
  
  Layer 1 — V8 Engine
&lt;/h1&gt;

&lt;p&gt;The V8 Engine is Google's open-source JavaScript engine.&lt;/p&gt;

&lt;p&gt;Its responsibility is &lt;strong&gt;very small but extremely important.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It executes JavaScript.&lt;/p&gt;

&lt;p&gt;Nothing more.&lt;/p&gt;

&lt;p&gt;V8 understands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables&lt;/li&gt;
&lt;li&gt;Objects&lt;/li&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;Classes&lt;/li&gt;
&lt;li&gt;Loops&lt;/li&gt;
&lt;li&gt;Promises&lt;/li&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;li&gt;Arithmetic&lt;/li&gt;
&lt;li&gt;Memory management&lt;/li&gt;
&lt;li&gt;Garbage Collection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&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="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;V8 can execute this entirely on its own.&lt;/p&gt;

&lt;p&gt;No operating system interaction is required.&lt;/p&gt;




&lt;h1&gt;
  
  
  What V8 Cannot Do
&lt;/h1&gt;

&lt;p&gt;Now consider this code.&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;fs&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;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data.txt&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;Can V8 read files?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Can V8 create an HTTP server?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Can V8 open TCP sockets?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Can V8 communicate with the operating system?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;This surprises many beginners.&lt;/p&gt;

&lt;p&gt;V8 is &lt;strong&gt;not&lt;/strong&gt; Node.js.&lt;/p&gt;

&lt;p&gt;It is only a JavaScript engine.&lt;/p&gt;




&lt;h1&gt;
  
  
  Layer 2 — Node APIs
&lt;/h1&gt;

&lt;p&gt;This is where Node becomes powerful.&lt;/p&gt;

&lt;p&gt;Node provides JavaScript with additional capabilities.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fs

http

https

path

crypto

stream

dns

os

net

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

&lt;/div&gt;



&lt;p&gt;These are called &lt;strong&gt;Node APIs&lt;/strong&gt; or &lt;strong&gt;Node Core Modules&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They are &lt;strong&gt;not JavaScript language features.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;They exist because Node adds them.&lt;/p&gt;

&lt;p&gt;For example,&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is &lt;strong&gt;not&lt;/strong&gt; part of JavaScript.&lt;/p&gt;

&lt;p&gt;Node provides it.&lt;/p&gt;

&lt;p&gt;Without Node,&lt;/p&gt;

&lt;p&gt;this API doesn't exist.&lt;/p&gt;




&lt;h1&gt;
  
  
  Layer 3 — C++ Bindings
&lt;/h1&gt;

&lt;p&gt;Now imagine this line.&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data.txt&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;JavaScript itself has absolutely no idea how to read a file.&lt;/p&gt;

&lt;p&gt;Think about it.&lt;/p&gt;

&lt;p&gt;How can JavaScript know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;where the file is stored?&lt;/li&gt;
&lt;li&gt;which SSD sector contains it?&lt;/li&gt;
&lt;li&gt;how Windows reads files?&lt;/li&gt;
&lt;li&gt;how Linux reads files?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It can't.&lt;/p&gt;

&lt;p&gt;So Node translates JavaScript into native code.&lt;/p&gt;

&lt;p&gt;This translation layer is called &lt;strong&gt;C++ Bindings&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of it like a translator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript

↓

"I want to read a file."

↓

C++

↓

Operating System understands.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Node itself is largely written in &lt;strong&gt;C++&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;These bindings connect JavaScript with native system functions.&lt;/p&gt;




&lt;h1&gt;
  
  
  Layer 4 — libuv
&lt;/h1&gt;

&lt;p&gt;This is probably the most important component of Node.js.&lt;/p&gt;

&lt;p&gt;And surprisingly...&lt;/p&gt;

&lt;p&gt;many Node developers never learn what it actually is.&lt;/p&gt;

&lt;h2&gt;
  
  
  So what is libuv?
&lt;/h2&gt;

&lt;p&gt;libuv is a &lt;strong&gt;cross-platform asynchronous I/O library&lt;/strong&gt; written in C.&lt;/p&gt;

&lt;p&gt;That definition sounds intimidating.&lt;/p&gt;

&lt;p&gt;Let's simplify it.&lt;/p&gt;

&lt;p&gt;Imagine Node directly talking to Windows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node

↓

Windows APIs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine the same Node application running on Linux.&lt;/p&gt;

&lt;p&gt;Linux has completely different system APIs.&lt;/p&gt;

&lt;p&gt;Node would need separate implementations for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Windows&lt;/li&gt;
&lt;li&gt;Linux&lt;/li&gt;
&lt;li&gt;macOS&lt;/li&gt;
&lt;li&gt;BSD&lt;/li&gt;
&lt;li&gt;Unix&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That would be a maintenance nightmare.&lt;/p&gt;

&lt;p&gt;Instead,&lt;/p&gt;

&lt;p&gt;Node talks only to libuv.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node

↓

libuv

↓

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

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node

↓

libuv

↓

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

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Node

↓

libuv

↓

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

&lt;/div&gt;



&lt;p&gt;libuv hides operating system differences.&lt;/p&gt;

&lt;p&gt;Your JavaScript remains identical.&lt;/p&gt;




&lt;h1&gt;
  
  
  Layer 5 — Operating System
&lt;/h1&gt;

&lt;p&gt;Now the request finally reaches the operating system.&lt;/p&gt;

&lt;p&gt;Suppose your code is&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;photo.png&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;The OS now becomes responsible for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;locating the file&lt;/li&gt;
&lt;li&gt;checking permissions&lt;/li&gt;
&lt;li&gt;communicating with the file system&lt;/li&gt;
&lt;li&gt;requesting data from storage&lt;/li&gt;
&lt;li&gt;returning the bytes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Node itself never searches your SSD.&lt;/p&gt;

&lt;p&gt;The operating system does.&lt;/p&gt;




&lt;h1&gt;
  
  
  Layer 6 — Hardware
&lt;/h1&gt;

&lt;p&gt;Finally...&lt;/p&gt;

&lt;p&gt;the storage device receives the request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SSD

↓

Read bytes

↓

Operating System

↓

libuv

↓

Node

↓

JavaScript Callback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only after this entire journey does your JavaScript receive the file.&lt;/p&gt;




&lt;h1&gt;
  
  
  Complete Flow
&lt;/h1&gt;

&lt;p&gt;Let's put everything together.&lt;/p&gt;

&lt;p&gt;Suppose we execute:&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;fs&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;fs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello.txt&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="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&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="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&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;p&gt;The internal flow looks 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;JavaScript

↓

V8 executes JavaScript

↓

Node API (fs)

↓

C++ Binding

↓

libuv

↓

Operating System

↓

SSD

↓

Operating System

↓

libuv

↓

C++ Binding

↓

JavaScript Callback

↓

console.log()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something interesting.&lt;/p&gt;

&lt;p&gt;The request travels &lt;strong&gt;down&lt;/strong&gt; through every layer.&lt;/p&gt;

&lt;p&gt;The result travels &lt;strong&gt;back up&lt;/strong&gt; through every layer.&lt;/p&gt;

&lt;p&gt;Node acts as a bridge between JavaScript and the operating system.&lt;/p&gt;




&lt;h1&gt;
  
  
  An Important Observation
&lt;/h1&gt;

&lt;p&gt;Many tutorials say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Node.js executes JavaScript."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Technically...&lt;/p&gt;

&lt;p&gt;that's not completely accurate.&lt;/p&gt;

&lt;p&gt;A more precise statement is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The V8 Engine executes JavaScript. Node.js provides the runtime environment, native APIs, C++ bindings, and operating system integration that allow JavaScript to perform tasks beyond the language itself.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This distinction matters.&lt;/p&gt;

&lt;p&gt;Understanding it changes how you see Node forever.&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Today we followed a single JavaScript statement all the way from your code to the hardware.&lt;/p&gt;

&lt;p&gt;We learned that Node.js is not simply "JavaScript outside the browser."&lt;/p&gt;

&lt;p&gt;It is a carefully designed runtime built from multiple independent components working together:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;V8 executes JavaScript.&lt;/li&gt;
&lt;li&gt;Node APIs expose new capabilities.&lt;/li&gt;
&lt;li&gt;C++ Bindings translate JavaScript into native operations.&lt;/li&gt;
&lt;li&gt;libuv provides cross-platform asynchronous I/O.&lt;/li&gt;
&lt;li&gt;The Operating System performs system-level work.&lt;/li&gt;
&lt;li&gt;The Hardware ultimately executes the request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every file read...&lt;/p&gt;

&lt;p&gt;every HTTP request...&lt;/p&gt;

&lt;p&gt;every database connection...&lt;/p&gt;

&lt;p&gt;every Express server...&lt;/p&gt;

&lt;p&gt;travels through this architecture.&lt;/p&gt;

&lt;p&gt;Understanding these layers is the difference between using Node.js...&lt;/p&gt;

&lt;p&gt;and understanding Node.js.&lt;/p&gt;




&lt;h2&gt;
  
  
  Coming Next
&lt;/h2&gt;

&lt;p&gt;In Part 3, we'll answer one of the most misunderstood questions in backend development:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why is Node.js asynchronous, and what really happens inside &lt;code&gt;fs.readFile()&lt;/code&gt; versus &lt;code&gt;fs.readFileSync()&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We'll explore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blocking vs Non-Blocking I/O&lt;/li&gt;
&lt;li&gt;Synchronous execution&lt;/li&gt;
&lt;li&gt;Asynchronous execution&lt;/li&gt;
&lt;li&gt;The Event Loop (from first principles)&lt;/li&gt;
&lt;li&gt;Why Node.js doesn't stop while waiting for I/O&lt;/li&gt;
&lt;li&gt;Where libuv actually fits into the story&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of memorizing "Node is asynchronous," we'll understand &lt;em&gt;why&lt;/em&gt; it works that way.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>javascript</category>
      <category>node</category>
      <category>programming</category>
    </item>
    <item>
      <title>From JavaScript to Node.js: Understanding What Really Happens Behind the Scenes (Part 1)</title>
      <dc:creator>Sudhanshu code</dc:creator>
      <pubDate>Fri, 24 Jul 2026 12:03:43 +0000</pubDate>
      <link>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-1-1kc</link>
      <guid>https://dev.to/sudhanshu_code_b8ef988ceb/from-javascript-to-nodejs-understanding-what-really-happens-behind-the-scenes-part-1-1kc</guid>
      <description>&lt;h1&gt;
  
  
  From JavaScript to Node.js: Understanding What Really Happens Behind the Scenes (Part 1)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Stop Memorizing Node.js. Start Understanding It.
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"I don't want to learn Node.js just to build APIs. I want to understand what actually happens behind the scenes."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That thought completely changed the way I approached backend development.&lt;/p&gt;

&lt;p&gt;Today, we live in a world where AI can generate hundreds of lines of code in seconds. We can build applications faster than ever before. But there is one thing AI cannot replace—our understanding of how software actually works.&lt;/p&gt;

&lt;p&gt;Anyone can copy code.&lt;/p&gt;

&lt;p&gt;Anyone can ask ChatGPT to generate an Express server.&lt;/p&gt;

&lt;p&gt;But when production crashes at 2 AM...&lt;br&gt;
when memory usage suddenly spikes...&lt;br&gt;
when thousands of requests hit your server...&lt;br&gt;
or when an interview asks, &lt;em&gt;"What actually happens after running &lt;code&gt;node app.js&lt;/code&gt;?"&lt;/em&gt;...&lt;/p&gt;

&lt;p&gt;Only fundamentals can help.&lt;/p&gt;

&lt;p&gt;This article is not about learning Node.js syntax.&lt;/p&gt;

&lt;p&gt;It is about understanding &lt;strong&gt;why Node.js exists&lt;/strong&gt;, &lt;strong&gt;how it works internally&lt;/strong&gt;, and &lt;strong&gt;how JavaScript reaches your hardware&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you're willing to learn from first principles, let's begin.&lt;/p&gt;


&lt;h1&gt;
  
  
  Every Computer Speaks Only One Language
&lt;/h1&gt;

&lt;p&gt;Before learning Node.js, we need to understand something even more fundamental.&lt;/p&gt;

&lt;p&gt;What language does a computer understand?&lt;/p&gt;

&lt;p&gt;Not JavaScript.&lt;/p&gt;

&lt;p&gt;Not Java.&lt;/p&gt;

&lt;p&gt;Not Python.&lt;/p&gt;

&lt;p&gt;Not C++.&lt;/p&gt;

&lt;p&gt;A computer understands only one language:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Machine Language

1010101010
1100101011
0001110101
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything eventually becomes binary.&lt;/p&gt;

&lt;p&gt;When your CPU executes instructions, it is not reading JavaScript.&lt;/p&gt;

&lt;p&gt;It is reading machine instructions represented as 0s and 1s.&lt;/p&gt;

&lt;p&gt;This immediately raises an important question.&lt;/p&gt;

&lt;p&gt;If computers only understand binary...&lt;/p&gt;

&lt;p&gt;How do programming languages work?&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Programming Languages Exist
&lt;/h1&gt;

&lt;p&gt;Imagine writing an entire operating system using binary.&lt;/p&gt;

&lt;p&gt;Instead of writing&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="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;Hello World&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 would write thousands of bits manually.&lt;/p&gt;

&lt;p&gt;Impossible.&lt;/p&gt;

&lt;p&gt;Programming languages were created to allow humans to communicate with computers more efficiently.&lt;/p&gt;

&lt;p&gt;Languages like&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C&lt;/li&gt;
&lt;li&gt;C++&lt;/li&gt;
&lt;li&gt;Java&lt;/li&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;JavaScript&lt;/li&gt;
&lt;li&gt;Rust&lt;/li&gt;
&lt;li&gt;Go&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;are &lt;strong&gt;human-friendly&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But computers still require machine code.&lt;/p&gt;

&lt;p&gt;Something must translate our code.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Translator Between Humans and Computers
&lt;/h1&gt;

&lt;p&gt;Different languages use different translation models.&lt;/p&gt;

&lt;p&gt;Let's understand three important ones.&lt;/p&gt;




&lt;h2&gt;
  
  
  C Language
&lt;/h2&gt;

&lt;p&gt;Suppose we write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&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;p&gt;Your CPU cannot execute this file directly.&lt;/p&gt;

&lt;p&gt;It first goes through a compiler.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;hello.c
      │
      ▼
 GCC Compiler
      │
      ▼
Machine Code (.exe)
      │
      ▼
CPU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The compiler converts the &lt;strong&gt;entire program&lt;/strong&gt; into machine code before execution begins.&lt;/p&gt;

&lt;p&gt;This is one reason why C programs are extremely fast.&lt;/p&gt;

&lt;p&gt;The CPU executes machine instructions directly.&lt;/p&gt;




&lt;h1&gt;
  
  
  Java
&lt;/h1&gt;

&lt;p&gt;Java follows a different approach.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Main.java
      │
      ▼
javac
      │
      ▼
Bytecode (.class)
      │
      ▼
JVM
      │
      ▼
Machine Code
      │
      ▼
CPU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of producing native machine code immediately, Java first creates &lt;strong&gt;Bytecode&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The JVM (Java Virtual Machine) then converts bytecode into machine instructions for the current operating system.&lt;/p&gt;

&lt;p&gt;That's why Java follows the famous principle:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Write Once, Run Anywhere.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Windows has its own JVM.&lt;/p&gt;

&lt;p&gt;Linux has its own JVM.&lt;/p&gt;

&lt;p&gt;macOS has its own JVM.&lt;/p&gt;

&lt;p&gt;Your Java code remains the same.&lt;/p&gt;




&lt;h1&gt;
  
  
  JavaScript Is Different
&lt;/h1&gt;

&lt;p&gt;Now comes JavaScript.&lt;/p&gt;

&lt;p&gt;Suppose we write:&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="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;Hello World&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;Can the CPU execute this?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;The CPU still understands only binary.&lt;/p&gt;

&lt;p&gt;So who executes JavaScript?&lt;/p&gt;

&lt;p&gt;The answer depends on where JavaScript is running.&lt;/p&gt;




&lt;h1&gt;
  
  
  JavaScript Inside the Browser
&lt;/h1&gt;

&lt;p&gt;Every modern browser ships with its own JavaScript engine.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Browser&lt;/th&gt;
&lt;th&gt;JavaScript Engine&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Chrome&lt;/td&gt;
&lt;td&gt;V8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Microsoft Edge&lt;/td&gt;
&lt;td&gt;V8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Firefox&lt;/td&gt;
&lt;td&gt;SpiderMonkey&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Safari&lt;/td&gt;
&lt;td&gt;JavaScriptCore&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Opera&lt;/td&gt;
&lt;td&gt;V8&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A JavaScript engine's primary job is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Execute JavaScript code as efficiently as possible.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When you open a website,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript
      │
      ▼
JavaScript Engine
      │
      ▼
Machine Code
      │
      ▼
CPU
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The engine converts your JavaScript into instructions that the processor can execute.&lt;/p&gt;

&lt;p&gt;Chrome uses V8.&lt;/p&gt;

&lt;p&gt;Firefox uses SpiderMonkey.&lt;/p&gt;

&lt;p&gt;Safari uses JavaScriptCore.&lt;/p&gt;

&lt;p&gt;The language remains JavaScript.&lt;/p&gt;

&lt;p&gt;Only the engine changes.&lt;/p&gt;




&lt;h1&gt;
  
  
  Does JavaScript Behave Differently Across Browsers?
&lt;/h1&gt;

&lt;p&gt;Sometimes.&lt;/p&gt;

&lt;p&gt;Not because JavaScript itself changes...&lt;/p&gt;

&lt;p&gt;but because browsers expose different APIs and may implement certain features differently.&lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window

document

navigator

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

&lt;/div&gt;



&lt;p&gt;are &lt;strong&gt;Browser APIs&lt;/strong&gt;, not JavaScript language features.&lt;/p&gt;

&lt;p&gt;Similarly,&lt;/p&gt;

&lt;p&gt;rendering engines,&lt;/p&gt;

&lt;p&gt;DOM implementation,&lt;/p&gt;

&lt;p&gt;CSS behavior,&lt;/p&gt;

&lt;p&gt;and browser optimizations&lt;/p&gt;

&lt;p&gt;may vary slightly across browsers.&lt;/p&gt;

&lt;p&gt;Modern browsers follow ECMAScript standards closely, so core JavaScript behavior is largely consistent today.&lt;/p&gt;




&lt;h1&gt;
  
  
  The Problem Before Node.js
&lt;/h1&gt;

&lt;p&gt;Before 2009...&lt;/p&gt;

&lt;p&gt;JavaScript had one major limitation.&lt;/p&gt;

&lt;p&gt;It could only run inside browsers.&lt;/p&gt;

&lt;p&gt;Think about it.&lt;/p&gt;

&lt;p&gt;JavaScript could manipulate HTML.&lt;/p&gt;

&lt;p&gt;It could respond to button clicks.&lt;/p&gt;

&lt;p&gt;It could create animations.&lt;/p&gt;

&lt;p&gt;But could JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read files from your computer?&lt;/li&gt;
&lt;li&gt;Create an HTTP server?&lt;/li&gt;
&lt;li&gt;Listen on port 3000?&lt;/li&gt;
&lt;li&gt;Access the operating system?&lt;/li&gt;
&lt;li&gt;Build backend applications?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;Because browsers intentionally restrict JavaScript.&lt;/p&gt;

&lt;p&gt;This restriction is called a &lt;strong&gt;sandbox&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why Browsers Restrict JavaScript
&lt;/h1&gt;

&lt;p&gt;Imagine visiting an unknown website.&lt;/p&gt;

&lt;p&gt;Without restrictions, that website could:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Delete your files&lt;/li&gt;
&lt;li&gt;Read your passwords&lt;/li&gt;
&lt;li&gt;Format your hard drive&lt;/li&gt;
&lt;li&gt;Install malware&lt;/li&gt;
&lt;li&gt;Access private documents&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That would be catastrophic.&lt;/p&gt;

&lt;p&gt;Browsers isolate JavaScript inside a secure environment.&lt;/p&gt;

&lt;p&gt;Inside a browser, JavaScript receives only browser-related capabilities such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window

document

history

location

navigator

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

&lt;/div&gt;



&lt;p&gt;But it cannot freely communicate with your operating system.&lt;/p&gt;

&lt;p&gt;This design keeps users safe.&lt;/p&gt;




&lt;h1&gt;
  
  
  Then Came Ryan Dahl
&lt;/h1&gt;

&lt;p&gt;In 2009, software engineer &lt;strong&gt;Ryan Dahl&lt;/strong&gt; looked at Google's V8 engine and had a simple but powerful idea.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"If V8 can execute JavaScript so efficiently inside Chrome...&lt;br&gt;
why can't it execute JavaScript outside the browser?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The engine already existed.&lt;/p&gt;

&lt;p&gt;It was incredibly fast.&lt;/p&gt;

&lt;p&gt;The missing piece was an environment that could provide operating-system capabilities.&lt;/p&gt;

&lt;p&gt;That idea eventually became &lt;strong&gt;Node.js&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  So, What Is Node.js?
&lt;/h1&gt;

&lt;p&gt;One of the biggest misconceptions is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Node.js is a programming language."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Wrong.&lt;/p&gt;

&lt;p&gt;Another misconception:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Node.js is a framework."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also wrong.&lt;/p&gt;

&lt;p&gt;The most accurate definition is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Node.js is a JavaScript runtime environment that embeds Google's V8 JavaScript engine and provides additional APIs for interacting with the operating system.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's simplify that.&lt;/p&gt;

&lt;p&gt;Node.js contains several important components working together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Your JavaScript
        │
        ▼
      V8 Engine
        │
        ▼
 Node.js Runtime
        │
        ▼
Operating System
        │
        ▼
Hardware
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The V8 engine executes JavaScript.&lt;/p&gt;

&lt;p&gt;Node.js provides everything JavaScript needs beyond the language itself.&lt;/p&gt;




&lt;h1&gt;
  
  
  What Does Node.js Add?
&lt;/h1&gt;

&lt;p&gt;JavaScript alone cannot do 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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nor 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="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nor 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="nx"&gt;net&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createConnection&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those are &lt;strong&gt;Node APIs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Node.js extends JavaScript by exposing operating-system functionality.&lt;/p&gt;

&lt;p&gt;Some examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File System (&lt;code&gt;fs&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;HTTP (&lt;code&gt;http&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Path (&lt;code&gt;path&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Process (&lt;code&gt;process&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Streams (&lt;code&gt;stream&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Crypto (&lt;code&gt;crypto&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Timers&lt;/li&gt;
&lt;li&gt;Networking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without Node.js, these APIs do not exist.&lt;/p&gt;




&lt;h1&gt;
  
  
  Browser vs Node.js
&lt;/h1&gt;

&lt;p&gt;Although both execute JavaScript, their environments are completely different.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Browser&lt;/th&gt;
&lt;th&gt;Node.js&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Runs inside browser&lt;/td&gt;
&lt;td&gt;Runs outside browser&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Has &lt;code&gt;window&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Has &lt;code&gt;global&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Has DOM&lt;/td&gt;
&lt;td&gt;No DOM&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can manipulate HTML&lt;/td&gt;
&lt;td&gt;Cannot manipulate HTML&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Browser APIs&lt;/td&gt;
&lt;td&gt;Operating System APIs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Focused on UI&lt;/td&gt;
&lt;td&gt;Focused on servers and backend&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This explains why the following code works inside Chrome:&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;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&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;but fails inside Node.js.&lt;/p&gt;

&lt;p&gt;Likewise,&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="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;location&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;exists in browsers...&lt;/p&gt;

&lt;p&gt;but not in Node.js.&lt;/p&gt;

&lt;p&gt;Different environments provide different capabilities.&lt;/p&gt;




&lt;h1&gt;
  
  
  A Different Way to Think About Node.js
&lt;/h1&gt;

&lt;p&gt;Most tutorials describe Node.js like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Node.js allows JavaScript to run outside the browser."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That statement is true.&lt;/p&gt;

&lt;p&gt;But it is incomplete.&lt;/p&gt;

&lt;p&gt;A better way to think about it is this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Node.js is the bridge between JavaScript and your operating system.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Without Node.js,&lt;/p&gt;

&lt;p&gt;JavaScript can think.&lt;/p&gt;

&lt;p&gt;With Node.js,&lt;/p&gt;

&lt;p&gt;JavaScript can act.&lt;/p&gt;

&lt;p&gt;It can create servers.&lt;/p&gt;

&lt;p&gt;Read files.&lt;/p&gt;

&lt;p&gt;Communicate over networks.&lt;/p&gt;

&lt;p&gt;Manage processes.&lt;/p&gt;

&lt;p&gt;Interact with the operating system.&lt;/p&gt;

&lt;p&gt;And eventually power applications used by millions of people.&lt;/p&gt;




&lt;h1&gt;
  
  
  What's Next?
&lt;/h1&gt;

&lt;p&gt;So far, we've answered one important question:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why does Node.js exist?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the next part, we'll answer a much deeper one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;What actually happens after running &lt;code&gt;node app.js&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We'll explore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;V8 Engine&lt;/li&gt;
&lt;li&gt;Node Runtime&lt;/li&gt;
&lt;li&gt;Node APIs&lt;/li&gt;
&lt;li&gt;C++ Bindings&lt;/li&gt;
&lt;li&gt;libuv&lt;/li&gt;
&lt;li&gt;Operating System&lt;/li&gt;
&lt;li&gt;Hardware&lt;/li&gt;
&lt;li&gt;The complete execution flow of JavaScript inside Node.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of memorizing Node.js...&lt;/p&gt;

&lt;p&gt;we're going to understand it from the inside out.&lt;/p&gt;




&lt;h3&gt;
  
  
  Thank you for reading.
&lt;/h3&gt;

&lt;p&gt;If this article helped you understand Node.js from first principles, stay tuned for Part 2, where we'll travel through every internal layer between your JavaScript code and the operating system.&lt;/p&gt;

</description>
      <category>backend</category>
      <category>javascript</category>
      <category>node</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
