Sept 11, 2019

5 things you probably don't know about Node.js

A few days ago, i was reading an article about how works Node.js child process and how the main process keep running in one core. After that, I stared to dig deeper in the Node.js repo to learning more about its internal behavior. These type of things, are the things you‘ll never find in an online course. That's why today i'm writing this post.

Most learning resources available on the internet (specially the online courses) about Node.js always teach about external packages and not about its runtime environment. The developers who are satisfied just with those resources, let’s consider them hypothetically “average”.

Now, we will learn about some tricky things about Node itself, which level up our proficiency with this ecosystem.

The CallStack is part of V8 Engine

Yes, it is. The V8 uses the Callstack as part of its architecture to keep the track of the functions invocations. So, what is the CallStack? It’s a data structure that records all the function calls, every time we invoke a function it take a references and keep doing so for each nested others invocation, including those who makes recursion.

From "Philip Roberts: What the heck is the event loop anyway" at JSConf EU

Is important to keep the Callstack not too much busy because there are just one CallStack available per Node process. Moreover, it just pop the nested function when this one finish and return a value.

So, The Event Loop is inside V8 too?

No. The event loop is provide by the libuv library. It is in charge to handle the external events who need of Node API, picking from event queues and push their callbacks into the Callstack. It acts like a guide, with functions like setTimeout or fs.readFile that belong to Node API, not to Javascript. It also decide what to execute next in the CallStack when is empty.

Node.js event loop architecture - Andranik Keshishyan

Why Node.js exit?

When a Node program runs, It will start the event loop automatically, and keep iterating while there something in execution, So, when the Call Stack and the Event Loop don’t have nothing else to do, the process exit. Node keep the process running when you start a timer or an HTTP server.

Node external dependencies

The node.js environment has a few libraries to help of the low-level operations of javascript. These are the libraries we have available:

  • libuv
  • http-parser
  • c-ares
  • OpenSSL (crypto)
  • Zlib

All of them are external to Node. They have their own source code. They have their own license. Node just uses them. Is good to learn this point, to be fair and not blame everything on Node ;).

Node.js could be used without V8

Node.js require a VM to run the main process but it is not necessarily V8. V8 is Google’s open source project not originally build by the node.js team. It’s not very common but You can use Chakra instead.

View Chakra repo on Github

Circular module dependencies are allowed

If you have two modules who require each other, will throw an error? Well no. In these cases, you will warned because when the circular require loop start, the first file will not be done yet, so the second file just will render as a partial version.

// module1.js


// module2.js


I hope you have learn new things and they can help you in your journey working with this amazing environment.

If you felt exiting about these type of topics, let me know with a comment.

5 things you probably don't know about Node.js