The Internals of Node.js

Part 1. Introduction

Song Cho
4 min readJun 1, 2019

1. The Basics of Node.js

What is Node.js?

Node.js is an open-source server environment, runs on various platforms (Windows, Linux, Mac), uses JavaScript on the server, and is free.

Sounds great, but what does that mean? How does that work?

“Starting with Node Internals

We’re going to start looking at the internals of Node.js with a very simple diagram.

NodeJS internally has a collection of dependencies that it uses to actually execute your code. Two of the most important dependencies are V8 and libuv.

  1. V8: is an open-source Javascript engine created by Google. It enables us to execute javascript code outside of the browser and that’s what we do when we run our javascript code from the terminal.
  2. libuv: is a C++ open-source project that gives Node.js access to the operating systems underlying file system. It gives us access to networking and it also handles some aspects of concurrency as well.

“Why we use Node.js again?”

important! : You might wonder why we don’t directly use V8 or libuv. Please look at the previous diagram and hope you get the idea.

  1. Node.js allows us to write a javascript code and it relates the javascript and the actual C++ to interpret and execute our Javascript code. (Note that V8 is like 70 percent C++ code and llibuvis 100 percent C++.)
  2. Node.js provides a series of wrappers and a very unified inconsistent API for us to use inside of our projects.

In short, Node.js gives us a nice consistent API for getting access to functionality that is ultimately implemented inside a V8 in libuv and so we don’t need to worry about the C++code! :)

2.Behind the scenes

To better understand how this actually works behind the scenes, let’s do an example.

We’re going to first start off by picking out some random function inside the node standard library and it’s going to be a function called pbksd2. This is a function inside of cryptolibrary usually used to hash a password for storage inside of a database.

So let’s take a look at the actual implementation of pbksd2 to inside the node code. When you installed node, you can find the ‘lib’ directory and the ‘src’ directory.

‘lib’ folder contains all the javascript definitions of functions and modules whereas ‘src’’ directory is the C++ implementation of all those javascript functions.

Ok, let’s try to understand all the process again with another diagram.

Assume we wrote a Javascript code and execute the process. Then Node.js will visit the javascript’s side of the node library to look up the pbksd2function, and the function called ‘process.binding()’ connect Javascript and C++ function.

“Node.js is backed by C++

This process.binding is what serves as an actual bridge between the javascript’s side of Node and the C++side. And V8 act as the intermediary and allow values that are defined inside of javascript to be translated into C++ equivalence.

Let’s pause it for now, we’re going to start to dive a little bit deeper intolibuv in the next blog.

Recap

  • The Node source code is written in Javascript and C++
  • V8 is used to interpret and execute Javascript code, while libuv is used for accessing the filesystem and some aspects of concurrency.

Reference

--

--