In this article, we will dive into the world of Node.js, learning what it is, why it was created, and what it is used for. This isn't a project-based tutorial – it aims to introduce beginners to Node and how it works.
Here are the topics we will cover:
- History of Node.js
- What is Node.js?
- How does Node.js work?
- Modules in Node.js
- Node.js looking forward
If learning about software tools and how they work is something you enjoy, then you'll enjoy reading this article. On that note, let's begin.
History of Node.js
In 2009, a software engineer named Ryan Dahl criticized the popular way back-end servers were run at the time. The most popular software for building Web Servers was the Apache HTTP Server. Dahl argued that it was limited, in that it could not handle a large number of real-time user connections (10,000 +) effectively.
Node.js was a great alternative to the traditional Apache HTTP server and slowly gained acceptance among the developer community.
Today, a lot of big organizations like Netflix, NASA, LinkedIn, Paypal, and many more use Node.js. These companies leverage Node.js’s capabilities to build robust applications for their users.
Also, in the most recent StackOverflow Developer Survey at the time of writing this article, Node.js ranked as the most popular technology in the "Web Frameworks and Technology" category. This goes on to show just how popular Node.js is now.
This article will go in-depth to look at what makes Node.js stand out, and how it works. But before that, we need to define exactly what it is.
From the Node.js official Website, it states that:
For us to define Node.js, we need to break the definition into parts. The terms we'll define are:
- Runtime Environment
What does open source mean?
Open source is generally used to describe software where the public can examine and edit its source code. This means anybody can inspect the code that makes the program work the way it does.
An advantage of this is that the users of the program get to understand it and its capabilities more. Also, if a person spots a bug, they can contribute and fix the bug.
You can find Node's Source Code on GitHub—the most popular website for displaying Open Source code. Node.js also has a lot of contributors—people who add features and fix bugs— on GitHub. Everyone has access to the source code of Node.js, and can even make their customized version of the program if they want to.
What does cross-platform mean?
If a program is cross-platform, it means that the program is not limited to a single Operating System or hardware architecture.
A cross-platform program can run on multiple platforms. Node.js runs on Windows, Linux, Unix, and MacOS among other platforms. Developers can use Node.js on a lot of operating systems.
What is a runtime environment?
How Does Node.js Work?
Node.js was written mostly with C/C++. As a program that is supposed to run web servers, Node.js needs to constantly interact with a device's operating system.
Building Node.js with a low-level language like C made it easy for the software to access the operating system’s resources and use them to execute instructions.
But there are many more intricacies involved in how Node.js works. Node.js runs fast and efficient web servers but how exactly does it do that? This section explains the process Node.js uses to achieve its efficiency.
There are three main components we must understand to see how Node.js works. These components are:
- V8 Engine
- Event Loop
We'll dive into detail and explain each of these components, and how they make up Node.js.
What is the V8 Engine?
Whenever the engine is executing code and comes across any of those variables, it looks up the actual value from the memory heap – just like whenever a hiker is feeling cold and wants to start a fire, they can look into their backpack for a lighter.
The call stack is another building block in the V8 engine. It is a data structure that manages the order of functions to be executed. Whenever the program invokes a function, the function is placed on the call stack and can only leave the stack when the engine has handled that function.
What is Libuv?
Apart from the V8 engine, another very important component of Node.js is Libuv. Libuv is a C library used for performing Input/output (I/O) operations.
I/O operations have to do with sending requests to the computer and receiving responses. These operations include reading and writing files, making network requests, and so on.
From Libuv’s official website, they state that:
Libuv is a multi-platform support Library with a focus on asynchronous I/O.
This means that Libuv is cross-platform (can run on any operating system) and has a focus on Asynchronous I/O.
The computer tends to take time to process I/O instructions, but Libuv—the library Node.js uses to interface with the computer— is focused on Asynchronous I/O. It can handle more than one I/O operation at once.
Whenever we pass a script to Node.js, the engine parses the code and starts processing it. The call stack holds the invoked functions and keeps track of the program. If the V8 engine comes across an I/O operation, it passes that operation over to Libuv. Libuv then executes the I/O operation.
Now, you don’t have to worry about these threads when using Node.js. Libuv knows how to manage them effectively. You just have to make use of the provided Node.js APIs to write the instructions.
Libuv was originally created for Node.js, but different programming languages now have bindings for it. Julia and Luvit (Lua-based Runtime Environment) have the bindings built in just like Node.js, but other languages have libraries that provide those bindings. An example is uvloop in Python, among others.
What is an Event Loop?
The Event Loop in Node.js is a very important part of the process. From the name, we can see it is a loop. The loop starts running as Node.js begins executing a program. In this section, we'll examine what the event loop does.
The Callback Queue works with the First In First Out (FIFO) approach. That means the first instruction (callback) to enter the queue is the first to be invoked.
Node.js is said to have an event-driven architecture. This means Node.js is built around listening to events and reacting to them promptly when they happen. These events can be timer events, network events, and so on.
Node.js responds to those events by using an event loop to load event callbacks to the engine after something triggers an event. It is for this reason that Node.js is excellent for real-time data transfer in applications.
Modules in Node.js
A lot of the functionality of Node.js is housed in modules that come with the software. These modules are meant to split the building blocks of programs into manageable chunks like Lego blocks. With this in place, we only have to import the modules we need for our programs.
For example, the piece of code below imports a built-in module called
const fs = require('node:fs')
But there are other ways we can use modules in Node.js. Apart from the built-in modules, we can also use modules (or packages) other developers built.
Node Package Manager (NPM) is a software application that comes together with Node.js. It manages all the third-party modules that are available in Node.js. Whenever you need a third-party package, you install it from NPM using the
npm install command.
To import a module you installed from NPM would look something like this:
const newModule = require('newModule')
Node.js Looking Forward
Node.js has a large community of developers now. It has thousands of contributors on GitHub and is used by some of the biggest companies today. But what does the future look like for Node.js?
Node.js has evolved well since it came into existence in 2009. It was originally made for back-end development, but it can do so much more now. You can use Node.js to develop desktop applications, front-end web applications, mobile applications, and command-line tools. Developers will continue to use it for more and more of these applications.
But Deno is yet to see massive adoption in the Developer Community. It is still a relatively new technology and needs more time to gain ground.
Also, the OpenJS Foundation which is the organization actively managing, developing, and maintaining Node.js has been fixing some of the bugs and the ultimate efficiency of Node.js. More projects are built on top of the Node.js architecture, and that will likely continue to be the case for the foreseeable future.
In this article, you have learned a lot and can now confidently answer the question “What exactly is Node.js?”.
We started by going over the history of Node.js, then properly defined Node.js. After that we elaborated on how Node.js works, explaining components such as the V8 Engine, Libuv, and Event Loop.
After that, we talked about modules in Node.js and NPM. Finally, we looked at what the future could hold for Node.js, and we concluded it will likely only power even more applications.
If you want to learn how to use Node.js to build applications, freeCodeCamp has an entire playlist of tutorials dedicated to it. There is an abundance of resources to learn the technology on the internet, and more are to come.
Good luck building your next application, and see you next time.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started