What Actually Happens When You Run a JS File in Node.js ?
before we dive into what nodejs does and how it does it. it is important that you are aware what nodejs really is.
what is node.js and what does it do?
node.js is not a JavaScript framework or a library , it’s a runtime environment for javascript.Node.js is written primarily in C and C++, but it embeds the V8 engine (also written in C++) and uses libuv (written in C).it solves a fundamental problem of running JavaScript out of a browser. and on top of this is provides web API’s and asynchronous i/o, ability to use JavaScript in the backend.
today because of node.js JavaScript is able to run on even small IOT devices
components of Node.js
- V8 engine (C++)
- libuv library (C language)
- Node core (C++)
how does it execute the JavaScript file ?
Before we go further, remember that JavaScript runs inside execution contexts managed by the engine.
let a=10;
console.log(a);so when you type this command in your terminal
node Test1.jsso to execute this file , node takes up the whole file and wraps it a IIFE like Wrapper function which is called a Module wrapper in nodejs terminology
(function (exports, require, module, __filename, __dirname) {
//Test1 file code
});why does node use module wrapper ?
- because it provides a module level scope, meaning variables won’t leak globally
- to inject special objects and functions into every file.
- enable the commonJs module system
- and because of this module wrapper , you are able to access require(), exports without any imports or anything.
so node does not execute your file as a global script , it executes it inside a module wrapper
- Node reads the file.
- Node wraps it in the module function.
- Node passes the wrapped code to V8.
- V8 compiles it to machine code.
- Execution begins.
and v8 executes it like any JavaScript line by line from top to bottom synchronously.
so what happens when v8 comes across async code ?
let’s take the following file as a example :
console.log("start");
setTimeout(() => {
console.log("inside timeout");
}, 1000);
console.log("end");If Node truly executed everything line by line synchronously, the timeout would block the program for one second. but it doesn’t!
Get vibhuti savaldekar’s stories in your inbox
Join Medium for free to get updates from this writer.
here is how this is handled :
- V8 begins executing synchronously.
- logs the first console statement “start”
V8 comes across setTimeout(), which is not originally a part of JavaScript.
yes even when you run JS on browser the setTimout() is a web api provided by the browser itself
And its the same in this case too, Node exposes the setTimeout API at the JavaScript level, but internally uses libuv to manage the timer mechanism.
- V8 immediately offloads timer operation to libuv.
- and V8 continues to execute the next line, “end” is logged in console.
- libuv keeps monitoring completed asynchronous operations.
When the timer expires, the callback is placed into the timers queue.
If the JavaScript call stack is empty, the event loop pushes the callback onto the stack. - and then “inside timeout” is logged.
the output at the end is this :
start
end
inside timeout





