<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by BINARYSPHERE on Medium]]></title>
        <description><![CDATA[Stories by BINARYSPHERE on Medium]]></description>
        <link>https://medium.com/@yadavsachin2446?source=rss-d802c3c84f1b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LT1xqxMYQHyLlZXhA-sFxA.jpeg</url>
            <title>Stories by BINARYSPHERE on Medium</title>
            <link>https://medium.com/@yadavsachin2446?source=rss-d802c3c84f1b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 12:45:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yadavsachin2446/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding JavaScript ArrayBuffer, DataView, and Byte Offsets]]></title>
            <link>https://medium.com/@yadavsachin2446/understanding-javascript-arraybuffer-dataview-and-byte-offsets-4d07f0eed674?source=rss-d802c3c84f1b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d07f0eed674</guid>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[BINARYSPHERE]]></dc:creator>
            <pubDate>Wed, 21 Oct 2026 18:02:04 GMT</pubDate>
            <atom:updated>2026-10-21T18:11:11.493Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PHRQZftv197sxjz3tKxZZA.png" /></figure><p>When learning JavaScript, we usually work with familiar data types such as strings, numbers, arrays, and objects.</p><p>For example:</p><pre>const name = &quot;JavaScript&quot;;<br>const age = 20;<br>const numbers = [10, 20, 30];</pre><p>JavaScript makes working with these values easy. But what happens when we need to work directly with binary data, such as the bytes inside a file or a message received over a network?</p><p>This is where concepts like ArrayBuffer, DataView, and byte offsets become important.</p><p>In this article, we’ll understand what they are, why they exist, and how they work together.</p><h3>1. Why Do We Need to Work With Binary Data?</h3><p>Computers ultimately store and process information as binary data. A byte consists of 8 bits, and each bit can be either 0 or 1.</p><p>For example, the decimal number 10 can be represented using one byte as:</p><pre>00001010</pre><p>When we use ordinary JavaScript values, we don’t usually need to think about how their data is represented at the byte level.</p><p>However, some tasks require direct access to bytes. Examples include:</p><ul><li>Reading and processing binary files.</li><li>Working with network protocols.</li><li>Handling data from hardware devices.</li><li>Processing images, audio, and other binary formats.</li><li>Communicating with systems that expect a particular binary format.</li></ul><p>JavaScript provides APIs for these tasks, including ArrayBuffer, typed arrays, and DataView.</p><p>Let’s understand each one.</p><h3>2. What Is an ArrayBuffer?</h3><p>An ArrayBuffer is an object representing a fixed-length region of raw binary data.</p><p>Think of it as a block of memory divided into individual bytes. When you create an ArrayBuffer, you specify how many bytes it should hold.</p><pre>const buffer = new ArrayBuffer(8);</pre><pre>console.log(buffer.byteLength);</pre><p>Output:</p><pre>8</pre><p>We created a buffer with a capacity of 8 bytes.</p><p>Visually, we can imagine it like this:</p><pre>ArrayBuffer: 8 bytes</pre><pre>┌────┬────┬────┬────┬────┬────┬────┬────┐<br>│ B0 │ B1 │ B2 │ B3 │ B4 │ B5 │ B6 │ B7 │<br>└────┴────┴────┴────┴────┴────┴────┴────┘<br>  0    1    2    3    4    5    6    7</pre><p>Each box represents one byte. The numbers below the boxes indicate their byte offsets from the beginning of the buffer.</p><p><strong>An important detail:</strong> An ArrayBuffer provides the storage, but it doesn&#39;t itself provide ordinary methods for reading a byte as an integer or interpreting multiple bytes as a number.</p><p>For example, this won’t work:</p><pre>const buffer = new ArrayBuffer(4);</pre><pre>console.log(buffer[0]); // undefined</pre><p>Why?</p><p>Because an ArrayBuffer isn&#39;t an ordinary JavaScript array. We need another way to interpret or access the bytes.</p><p>That brings us to DataView.</p><h3>3. What Is DataView?</h3><p>DataView is an interface that lets us read and write different numeric types in an ArrayBuffer.</p><p>We can create a DataView using an existing buffer:</p><pre>const buffer = new ArrayBuffer(4);<br>const view = new DataView(buffer);</pre><p>Here, buffer provides four bytes of storage, while view gives us methods to interpret those bytes as values.</p><p>For example:</p><pre>view.setInt8(0, 10);</pre><pre>console.log(view.getInt8(0));</pre><p>Output:</p><pre>10</pre><p>Let’s understand the two methods.</p><ul><li>setInt8() writes an 8-bit signed integer into the buffer.</li><li>getInt8() reads an 8-bit signed integer from the buffer.</li></ul><p>The first argument is the byte offset. The second argument of setInt8() is the value to write.</p><p>DataView also supports other types, including:</p><p>Integer methods</p><ul><li>getInt8() — Reads a signed 8-bit integer (1 byte).</li><li>getUint8() — Reads an unsigned 8-bit integer (1 byte).</li><li>getInt16() — Reads a signed 16-bit integer (2 bytes).</li><li>getUint16() — Reads an unsigned 16-bit integer (2 bytes).</li><li>getInt32() — Reads a signed 32-bit integer (4 bytes).</li></ul><p>Floating-point methods</p><ul><li>getFloat32() — Reads a 32-bit floating-point number (4 bytes).</li><li>getFloat64() — Reads a 64-bit floating-point number (8 bytes).</li></ul><p>Corresponding set...() methods are available for writing these values.</p><p>Notice that these methods interpret the same underlying bytes differently depending on the selected type.</p><p>For example, two bytes can be interpreted as one 16-bit integer or as two separate 8-bit values.</p><h3>4. What Is a Byte Offset?</h3><p>An offset tells us where an operation starts, measured in bytes.</p><p>Suppose we create a buffer with four bytes:</p><pre>const buffer = new ArrayBuffer(4);<br>const view = new DataView(buffer);</pre><p>Now let’s write two 16-bit integers:</p><pre>view.setInt16(0, 1000);<br>view.setInt16(2, 2000);</pre><p>Why did we use offsets 0 and 2?</p><p>Because a 16-bit integer occupies 16 bits, which equals 2 bytes.</p><p>Our buffer can be represented like this:</p><pre>Byte offset:  0       1       2       3<br>             ┌───────┬───────┬───────┬───────┐<br>             │       │       │       │       │<br>             └───────┴───────┴───────┴───────┘<br>             └── 1000 ──┘   └── 2000 ──┘</pre><p>The first integer occupies bytes 0 and 1. The second occupies bytes 2 and 3.</p><p>We can read them back:</p><pre>console.log(view.getInt16(0)); // 1000<br>console.log(view.getInt16(2)); // 2000</pre><p>The offset doesn’t mean the value of the number or the index of a JavaScript array. It identifies the starting byte position for the operation.</p><p>For example, getInt16(2) means:</p><p>“Start at byte offset 2 and interpret the next two bytes as a signed 16-bit integer.”</p><h3>What if we use the wrong offset?</h3><pre>view.setInt16(0, 1000);</pre><pre>console.log(view.getInt16(1));</pre><p>This reads two bytes starting at offset 1, overlapping the bytes used by the original value.</p><p>It may return a different number because the bytes are being interpreted from a different starting position.</p><p>This is why understanding offsets is important when working with binary formats.</p><h3>5. Big-Endian vs. Little-Endian</h3><p>When a value occupies multiple bytes, we need to know the order in which those bytes are arranged.</p><p>Consider the hexadecimal value:</p><pre>0x1234</pre><p>It consists of two bytes:</p><pre>12 34</pre><p>There are two common ways to arrange them.</p><p><strong>Big-endian:</strong></p><pre>Address:  0     1<br>        ┌────┬────┐<br>        │ 12 │ 34 │<br>        └────┴────┘</pre><p>The most significant byte comes first.</p><p><strong>Little-endian:</strong></p><pre>Address:  0     1<br>        ┌────┬────┐<br>        │ 34 │ 12 │<br>        └────┴────┘</pre><p>The least significant byte comes first.</p><p>DataView allows us to specify the byte order using its optional littleEndian argument.</p><pre>const buffer = new ArrayBuffer(2);<br>const view = new DataView(buffer);</pre><pre>view.setUint16(0, 0x1234, false);</pre><pre>console.log(view.getUint8(0).toString(16)); // 12<br>console.log(view.getUint8(1).toString(16)); // 34</pre><p>We passed false, which requests big-endian order.</p><p>Now try little-endian:</p><pre>view.setUint16(0, 0x1234, true);</pre><pre>console.log(view.getUint8(0).toString(16)); // 34<br>console.log(view.getUint8(1).toString(16)); // 12</pre><p>The underlying bytes are arranged differently, but reading the two bytes as a 16-bit integer with the matching byte order gives us the same original value.</p><p>One important detail: DataView uses big-endian by default when the optional byte-order argument is omitted. Typed arrays, such as Uint16Array, use the platform&#39;s native byte order instead.</p><h3>6. Putting Everything Together</h3><p>Let’s combine the concepts in one example.</p><pre>const buffer = new ArrayBuffer(4);<br>const view = new DataView(buffer);</pre><pre>// Write two 16-bit unsigned integers<br>view.setUint16(0, 1000, false);<br>view.setUint16(2, 2000, false);</pre><pre>// Read them back<br>const first = view.getUint16(0, false);<br>const second = view.getUint16(2, false);</pre><pre>console.log(first);<br>console.log(second);</pre><p>Output:</p><pre>1000<br>2000</pre><p>Here’s what happened:</p><ol><li>ArrayBuffer(4) allocated four bytes.</li><li>DataView gave us methods to interpret those bytes.</li><li>setUint16(0, 1000, false) wrote a 16-bit unsigned integer starting at byte 0.</li><li>setUint16(2, 2000, false) wrote another 16-bit unsigned integer starting at byte 2.</li><li>getUint16() read each value from its respective offset.</li></ol><p>The buffer provides the storage, the view interprets the data, and the offsets identify where each operation begins.</p><h3>7. ArrayBuffer vs. DataView vs. Typed Arrays</h3><p>You may also encounter typed arrays such as Uint8Array and Int16Array.</p><p>They are related, but they serve slightly different purposes.</p><p>FeatureArrayBufferDataViewTyped arrayMain purposeStore raw binary dataRead and write different types at chosen offsetsWork with elements of one specific typeDirect element accessNoThrough methodsYesDifferent types in one viewNot applicableYesEach typed array uses one element typeExplicit byte-order controlNoYesUses native byte order</p><p>For example, a Uint8Array is useful when you want to access each byte as an unsigned integer:</p><pre>const buffer = new ArrayBuffer(4);<br>const bytes = new Uint8Array(buffer);</pre><pre>bytes[0] = 10;<br>bytes[1] = 20;</pre><pre>console.log(bytes[0]); // 10<br>console.log(bytes[1]); // 20</pre><p>Use a typed array when you want convenient indexed access to values of one type. Use DataView when you need explicit control over offsets, numeric types, and byte order.</p><h3>Conclusion</h3><p>Working with binary data becomes easier when we understand the role of each concept.</p><ul><li><strong>ArrayBuffer</strong> represents a fixed-length region of raw binary data.</li><li><strong>DataView</strong> lets us read and write different numeric types within that data.</li><li><strong>Byte offsets</strong> tell us where a read or write operation begins.</li><li><strong>Endianness</strong> determines the byte order used to represent multibyte values.</li></ul><p>These concepts are useful when working with binary files, network protocols, hardware communication, and other low-level data formats.</p><p>The next time you see an ArrayBuffer in JavaScript, remember: the buffer provides the bytes, the view tells JavaScript how to interpret them, and the offset tells it where to start.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d07f0eed674" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Behind the Scenes of CommonJS: How Node.js Loads Your Code]]></title>
            <link>https://medium.com/@yadavsachin2446/behind-the-scenes-of-commonjs-how-node-js-loads-your-code-8121fefa5b93?source=rss-d802c3c84f1b------2</link>
            <guid isPermaLink="false">https://medium.com/p/8121fefa5b93</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[commonjs]]></category>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[BINARYSPHERE]]></dc:creator>
            <pubDate>Mon, 19 Oct 2026 15:23:51 GMT</pubDate>
            <atom:updated>2026-10-19T15:23:51.297Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gRSXnhsR1IBzW8rqMtJE3Q.png" /></figure><p><em>Understanding module objects, </em><em>module.exports, the wrapper function, and what happens when you run </em><em>node app.js.</em></p><p>When learning Node.js, we commonly use statements such as require(), module.exports, and exports. We use them to import and export code between files, but have you ever wondered what happens behind the scenes?</p><p>Where does the module object come from? Who provides require()? Why can we access __dirname without declaring it? And what exactly is the module wrapper function?</p><p>In this article, we will explore how the CommonJS module system works internally and how it fits into the Node.js runtime.</p><h3>1. What Is CommonJS?</h3><p>CommonJS is a module system traditionally used by Node.js to organize JavaScript code into reusable modules.</p><p>Consider a project with two files:</p><pre>project/<br>├── app.js<br>└── math.js</pre><p>Suppose math.js contains a function:</p><pre>// math.js<br>function add(a, b) {<br>    return a + b;<br>}</pre><pre>module.exports = add;</pre><p>We can use that function in app.js:</p><pre>// app.js<br>const add = require(&#39;./math&#39;);</pre><pre>console.log(add(10, 20)); // 30</pre><p>Here, module.exports makes the function available to other modules, while require() loads the exported value.</p><p>But neither the module object nor the CommonJS loading mechanism is something we had to implement ourselves. Node.js provides them as part of its runtime.</p><p>To understand how this works, we need to look beyond the syntax.</p><h3>2. The module Object</h3><p>When Node.js loads a CommonJS module, it creates a module object that represents that module.</p><p>You can inspect it:</p><pre>// app.js<br>console.log(module);</pre><p>The object contains properties describing the module and its relationships with other modules.</p><p>Some important properties are:</p><p>PropertyPurposemodule.idIdentifies the modulemodule.exportsHolds the value exported to other modulesmodule.filenameAbsolute path of the module filemodule.loadedIndicates whether the module has finished loadingmodule.childrenContains the module objects directly required by this modulemodule.pathsLists directories used when resolving packages</p><p>For example:</p><pre>console.log(module.id);<br>console.log(module.filename);<br>console.log(module.exports);</pre><p>The exact values depend on how the module is loaded and which Node.js version is used.</p><h3>Does every file have its own module object?</h3><p>For each CommonJS module loaded by Node.js, the loader creates a corresponding module object.</p><p>For example, if app.js requires math.js, Node.js maintains separate module objects for these two modules.</p><p>This helps Node.js track exports, loading status, and module dependencies.</p><h3>3. Understanding module.exports and exports</h3><p>These two names are closely related, but they are not interchangeable in every situation.</p><h3>module.exports</h3><p>The module.exports property determines what another module receives when it calls require().</p><pre>// math.js<br>function add(a, b) {<br>    return a + b;<br>}</pre><pre>module.exports = add;</pre><p>Then:</p><pre>// app.js<br>const add = require(&#39;./math&#39;);</pre><pre>console.log(add(5, 7)); // 12</pre><p>We replaced the default exported object with a function.</p><p>Remember that module.exports can contain a function, object, class, or another JavaScript value.</p><h3>exports</h3><p>Initially, Node.js provides exports as a shortcut referring to the same object as module.exports.</p><p>This allows us to add properties conveniently:</p><pre>// math.js<br>exports.add = (a, b) =&gt; a + b;<br>exports.subtract = (a, b) =&gt; a - b;</pre><p>Now another file can access both functions:</p><pre>const math = require(&#39;./math&#39;);</pre><pre>console.log(math.add(10, 5));      // 15<br>console.log(math.subtract(10, 5)); // 5</pre><h3>Why does reassignment behave differently?</h3><p>Consider:</p><pre>exports = {<br>    add: (a, b) =&gt; a + b<br>};</pre><p>This reassigns the local exports variable. It does not replace module.exports.</p><p>The distinction is:</p><ul><li>exports.add = add adds a property to the original exported object.</li><li>module.exports = add replaces the exported value.</li><li>exports = add only reassigns the local shortcut.</li></ul><p>Understanding this difference makes the CommonJS system much easier to reason about.</p><h3>4. The Module Wrapper Function</h3><p>Now we reach one of the most interesting parts of CommonJS: the module wrapper function.</p><p>Consider this ordinary JavaScript file:</p><pre>const message = &#39;Hello from Node.js&#39;;</pre><pre>console.log(message);</pre><p>In a CommonJS module, Node.js wraps the module’s source code in a function before executing it.</p><p>Conceptually, the wrapper looks like this:</p><pre>(function (exports, require, module, __filename, __dirname) {<br>    const message = &#39;Hello from Node.js&#39;;</pre><pre>    console.log(message);<br>});</pre><p>This is the CommonJS module wrapper.</p><p>The five parameters are:</p><p>ParameterPurposeexportsShortcut for adding properties to the exported valuerequireLoads other modulesmoduleRepresents the current module__filenameAbsolute path of the current file__dirnameAbsolute path of the directory containing the file</p><h3>Why does Node.js use this wrapper?</h3><p>The wrapper provides a separate function scope for each CommonJS module. As a result, top-level variables declared with var, let, or const do not automatically become properties of the global object.</p><p>For example:</p><pre>// first.js<br>const secret = 100;</pre><pre>// second.js<br>console.log(secret); // ReferenceError</pre><p>The variable secret is local to first.js. Another module cannot directly access it merely by requiring that file.</p><p>Only values exposed through the module’s exports are made available to importing code.</p><h3>Is the wrapper a separate file?</h3><p>No. You do not need to create a wrapper file in your project.</p><p>The wrapper is part of Node.js’s CommonJS loading mechanism. Node.js prepares the module code for execution using its internal loader.</p><p>The example above illustrates the wrapper’s structure; it is not a literal copy of every internal step performed by Node.js.</p><h3>5. Understanding __dirname and __filename</h3><p>These two variables help us determine where a CommonJS module is located.</p><p>Suppose the file is located at:</p><pre>/home/project/routes/user.js</pre><p>Inside user.js:</p><pre>console.log(__filename);<br>console.log(__dirname);</pre><p>The output would be:</p><pre>/home/project/routes/user.js<br>/home/project/routes</pre><p>The difference is straightforward:</p><ul><li>__filename gives the absolute path, including the filename.</li><li>__dirname gives the absolute path of the containing directory.</li></ul><p>These variables are particularly useful when working with files.</p><p>For example:</p><pre>const path = require(&#39;node:path&#39;);<br>const fs = require(&#39;node:fs&#39;);</pre><pre>const filePath = path.join(__dirname, &#39;data.txt&#39;);</pre><pre>const data = fs.readFileSync(filePath, &#39;utf8&#39;);</pre><pre>console.log(data);</pre><p>Using __dirname helps us construct a path relative to the current module instead of relying on the directory from which the Node.js command was launched.</p><p>These variables are specific to the CommonJS module environment. Native ECMAScript modules use alternatives such as import.meta.url.</p><h3>6. Parent and Child Modules</h3><p>Modules can depend on other modules.</p><p>Consider:</p><pre>project/<br>├── app.js<br>├── math.js<br>└── user.js</pre><p>Suppose app.js contains:</p><pre>require(&#39;./math&#39;);<br>require(&#39;./user&#39;);</pre><p>In this example, app.js directly requires math.js and user.js.</p><p>We can visualize the relationship like this:</p><pre>app.js<br>          /      \<br>      math.js   user.js</pre><p>Here, app.js is the parent in the dependency relationship, while math.js and user.js are its children.</p><p>Node.js exposes module relationship information through properties such as module.children.</p><p>Historically, module.parent was used to inspect the module that first required a module, but it is deprecated in modern Node.js. Also, parent-child relationships describe module loading, not simply which files share a folder.</p><h3>7. What Actually Happens When We Run node app.js?</h3><p>We now understand the individual pieces. Let’s connect them.</p><p>When you execute:</p><pre>node app.js</pre><p>Node.js starts the runtime and loads your entry file.</p><p>At a high level, the process looks like this:</p><p><strong>Step 1: Node.js starts.</strong></p><p>The Node.js executable initializes the runtime and its components.</p><p><strong>Step 2: The CommonJS loader prepares the module.</strong></p><p>For a CommonJS file, the loader resolves the file, creates its module object, and prepares its code for execution.</p><p><strong>Step 3: The wrapper provides the module scope.</strong></p><p>The code executes with access to the CommonJS parameters, including module, exports, require, __filename, and __dirname.</p><p><strong>Step 4: V8 executes the JavaScript.</strong></p><p>V8 is the JavaScript engine used by Node.js. It compiles and executes the JavaScript code.</p><p><strong>Step 5: Node.js records the module’s result.</strong></p><p>After a CommonJS module has loaded successfully, Node.js can reuse its cached module when it is required again, subject to the module cache’s behavior.</p><p>This is a simplified overview. The precise implementation involves additional details, including module resolution, error handling, and circular dependencies.</p><h3>8. Where Do V8 and libuv Fit In?</h3><p>Node.js is not just a JavaScript engine. It combines several components to provide a server-side JavaScript runtime.</p><h3>V8: The JavaScript engine</h3><p>V8 compiles and executes JavaScript.</p><p>It is responsible for running statements such as:</p><pre>console.log(&#39;Hello&#39;);</pre><pre>const result = 10 + 20;</pre><p>The CommonJS loader prepares the module, while V8 executes its JavaScript code.</p><h3>libuv: Asynchronous I/O and the event loop</h3><p>libuv provides infrastructure for the event loop, asynchronous I/O, and operations involving its thread pool.</p><p>Consider:</p><pre>const fs = require(&#39;node:fs&#39;);</pre><pre>console.log(&#39;Start&#39;);</pre><pre>fs.readFile(&#39;data.txt&#39;, &#39;utf8&#39;, (err, data) =&gt; {<br>    if (err) throw err;</pre><pre>    console.log(data);<br>});</pre><pre>console.log(&#39;End&#39;);</pre><p>The output will typically be:</p><pre>Start<br>End<br>(contents of data.txt)</pre><p>The filesystem operation completes asynchronously. Node.js coordinates the operation and invokes the callback when the result is ready.</p><p>The precise implementation varies by operation: some I/O is handled through operating-system facilities, while other work uses libuv’s thread pool.</p><p><strong>An important distinction:</strong> libuv does not create the CommonJS wrapper and does not execute JavaScript. V8 executes the JavaScript; libuv supports asynchronous operations and event-loop behavior.</p><h3>9. Can We Read the Actual Node.js Source Code?</h3><p>Yes! If you want to explore the implementation rather than just learn the public API, Node.js is open source.</p><p>Start here:</p><p><strong>CommonJS loader:</strong><br><a href="https://github.com/nodejs/node/blob/main/lib/internal/modules/cjs/loader.js">https://github.com/nodejs/node/blob/main/lib/internal/modules/cjs/loader.js</a></p><p>This source file contains the implementation of CommonJS loading and wrapper-related behavior.</p><p>For a meaningful investigation, choose a specific Node.js release and inspect the corresponding source. Internal details may change between releases, so avoid assuming that every implementation detail is identical across versions.</p><p>You can also inspect the official documentation:</p><p><a href="https://nodejs.org/api/modules.html">https://nodejs.org/api/modules.html</a></p><p>Reading the source code after understanding the concepts makes it much easier to connect the implementation with the behavior you observe in your programs.</p><h3>Conclusion</h3><p>CommonJS is more than just require() and module.exports. It is part of the mechanism Node.js uses to load, organize, and execute JavaScript modules.</p><p>The key ideas to remember are:</p><ul><li>Each loaded CommonJS module has its own module object.</li><li>module.exports determines what require() returns.</li><li>exports initially refers to the same object as module.exports.</li><li>The wrapper function provides module scope and access to CommonJS parameters.</li><li>__filename and __dirname provide file-location information.</li><li>The CommonJS loader handles module loading, V8 executes JavaScript, and libuv supports asynchronous I/O and the event loop.</li></ul><p>The next time you write require(&#39;./math&#39;), remember that there is a module-loading system working behind that simple line of code.</p><p>Understanding these internals helps you move from merely using Node.js to understanding how its runtime works.</p><p><em>Further reading: </em><a href="https://nodejs.org/api/modules.html"><em>Node.js CommonJS Modules Documentation</em></a><em> · </em><a href="https://nodejs.org/api/esm.html"><em>Node.js ECMAScript Modules Documentation</em></a><em> · </em><a href="https://github.com/nodejs/node"><em>Node.js Source Code</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8121fefa5b93" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>