Back to course

Diving into the NodpeJS Github repo

Updated at April 9, 2025

4 min read

Want to edit

image.png

The principle of leave privilege (PoLP)

As we know, each module in Node.js has its own scope. How does Node.js achieve this? In JavaScript, we follow the Principle of Least Privilege (PoLP), which is related to functions and scope. If you're unfamiliar, you can Google it. The idea is to only expose what is necessary to the global scope, keeping everything else private. To achieve PoLP, wrap your code in a function or immediately invoke it (IIFE). This is how modules in Node.js work. You can learn more about PoLP from this Stack Overflow post or watch this YouTube video. Here's a tip: mention PoLP in your next interview to impress the interviewer. Want a mock interview with me? Click here to schedule one now.

How modules and require work ?

Now you understand that when you require something in Node.js, the entire code is wrapped in a function (essentially an IIFE). This is how PoLP works, making the code private and inaccessible to other modules unless explicitly exported. The module keyword in your file is available because Node.js passes it as an object to the IIFE while converting your module code. Below are two examples: one without PoLP and one with PoLP. Remember, PoLP is just about hiding code from the outside environment.

Without PoLP

var x=10;
 // do something with x
 console.log('value is : ', x)
//Suppose till this scope you need x 

console.log(x) // 10; but here also you are getting x access thats not good

So to hide this we can wrap it in a function

function() {  // <--- Add this

 var x=10;
 // do something with x
  console.log('value is : ', x)
 //Suppose till this scope you need x 
 
 
 
 }()          // <----Add this ( thats it IIFE done)
 
 console.log(x)   // ERROR ( now you cant access because its hidden now,  private 
  )

Lets see how this works with modules in node js

function() {  

 var x=10;
 // do something with x
  console.log('value is : ', x)
 //Suppose till this scope you need x 
 
 modules.export= { x }
 
 }(modules)          // node js passing modules in IIFE while calling it
 
 console.log(x)   // ERROR ( now you cant access because its hidden now private now
  )

Behind the scenes, Node.js uses its capabilities, like wrapping code in an IIFE, and then the V8 engine executes the code. When you require('./sum.js') in another module, Node.js performs several steps to load and execute the module. There are five key steps involved in this process to ensure the module is properly loaded, compiled, and executed. Let's explore what these five steps are.

5 Steps to Load and Execute a Module

  1. Require a Module Identifies the source of the module, whether it's a local file, built-in module, or JSON file.
  2. Load the Module Accesses the module's content but does not execute it yet.
  3. Wrap in IIFE Wraps the module code in an Immediately Invoked Function Expression (IIFE), as previously discussed.
  4. Evaluate the Code Executes the code, allowing module.exports to return the exposed content.
  5. Caching Caches the module to avoid re-execution, improving performance by reusing the cached result in subsequent requires.

image.png

Let me introduce you with one of superpower of NodeJs, we will discuss it later.

image.png

Its time to open Github repo of Node JS and read about it. Here below you can see how NodeJS wraps the module code in IIFE , you can find this code here in node Js repo

let wrap = function(script) { // eslint-disable-line func-style
  return Module.wrapper[0] + script + Module.wrapper[1];
};

const wrapper = [
  '(function (exports, require, module, __filename, __dirname) { ',
  '\n});',
];

Search These Keywords

  1. IIFE (Immediately Invoked Function Expression)
  2. PoLP (Principle of Least Privilege)
  3. Caching in NodeJS
  4. LibUV Library

Questions

  1. How do you make a set of variables private?
  2. What are the use cases for IIFEs?
  3. Besides IIFEs, are there other methods to achieve the same functionality?
  4. Find the code in the Node.js repository where modules are wrapped in IIFE.
  1. NodeJS GitHub Repository (V8 Code)
  2. LibUV Code - The Powerhouse of NodeJS
  3. Require Function in Node.js Repository

Tips

  1. Open any GitHub repository and press the . (full stop) key on your keyboard to open it in a VS Code-like editor directly in your browser.
  2. Alternatively, add 1s before the URL in the repository link to open it in a browser-based editor. For example, https://github1s.com/.

And that's all for this episode!

I'm Rahul Aher, and I'm writing digital notes on Node.js. If you enjoy these notes, please share them with your friends. If you find any errors or have improvements, feel free to contribute by forking the repo. If you're interested in writing the next episode's notes, fork the repo and contribute. Let's learn together! Also, please consider giving a star to this repo. For any queries, let's connect here.

Take care, Good Bye :)

Thank you so much for reading. If you found it valuable, consider subscribing for more such content every week. If you have any questions or suggestions, please email me your comments or feel free to improve it.

Photo of Rahul Aher

Written by Rahul Aher

I'm Rahul, Sr. Software Engineer (SDE II) and passionate content creator. Sharing my expertise in software development to assist learners.

More about me