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.
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.
module.exports
to return the exposed content.Let me introduce you with one of superpower of NodeJs, we will discuss it later.
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});',
];
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.
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.
I'm Rahul, Sr. Software Engineer (SDE II) and passionate content creator. Sharing my expertise in software development to assist learners.
More about me