Complete Intro Nodejs
What And Why

The What and Why

Let’s start at the very beginning: What exactly is Node.js?

Node is usually defined as “JavaScript on backend servers”. Before Node, that was not a common or easy thing. JavaScript was mainly a frontend thing.

However, this definition isn’t really an accurate one because Node offers a lot more than executing JavaScript on servers. In fact, the execution of JavaScript is not done by Node at all. It’s done with a Virtual Machine (VM) like V8 or Chakra. Node is just the coordinator. It’s the one who instructs a VM like V8 to execute your JavaScript.

Node is better defined as a wrapper around a VM like V8.

V8 is Google’s open source JavaScript engine. It’s written in C++ and it’s used in Google Chrome and in Node.js. Both Chrome and Node use V8 to execute JavaScript code. V8 is the default VM in Node, but you can run Node with other VMs if you need to.

When you write JavaScript code and execute it with the node command, Node will pass your JavaScript to V8, V8 will execute that JavaScript and tell Node what the result is, and Node will make the result available to you.

That’s the simple story, but Node.js is more useful than just that. Node comes with some handy, built-in modules providing easy-to-use asynchronous APIs. Let’s talk about that, and a few other reasons why developers are picking Node.js over many other options when it comes to creating services for their backends.

1. Native Modules

Node comes with feature-rich modules that make it a great platform for hosting and managing servers. These modules offer features like reading and writing files, consuming data over the network, and even compressing and encrypting data You don’t need to install these modules. They come natively packaged with Node!

The big deal about these modules is that they offer asynchronous APIs that you can just use directly without worrying about threads. Yes! You can do asynchronous programming in Node and do things in parallel without needing to deal with threads. This is a big deal and it’s probably the most common benefit of using a runtime like Node.

The asynchronous nature of these modules works great with VMs like V8 because these VMs are all single-threaded. This is true for both Node and Browsers. You only get a single precious thread to work with. It’s extremely important to not block that thread. For example, in your browser, if your website blocks that single thread for, say 2 seconds, the user cannot scroll up and down during these 2 seconds! In Node, if an incoming HTTP connection to your web server was handled synchronously rather than asynchronously, that’ll block the single thread, and your whole web server cannot handle any other connections while the synchronous operation is active. That’s very bad.

If Node’s built-in modules were not enough for you, you can build high-performing packages using C++! Node has first-class support for C++ addons, creating dynamically linked shared objects that you can use directly with Node. Of course you can also write your addons in JavaScript if you want.

Node also ships with a powerful debugger and has some other handy, generic utilities that enhance the JavaScript language and provide extra APIs (for example, to create timers, work with data types, and process arrays and objects).

Even if you don’t want to host your backend servers in Node, having the powerful, built-in features — and the ease to add more — makes Node a great platform for tools to work with other applications and enhance your work flow.

2. A Package Manager and a Dependency Manager

Node ships with a powerful package manager (which is called NPM). We did not have a package manager in the JavaScript world before Node. NPM was nothing short of revolutionary. It changed the way we work and share JavaScript. Node was the enabler of this because NPM ships natively with Node.

NPM is basically the world’s largest collection of FREE and reusable code. You can make a feature-rich Node application just by using code that’s freely available on NPM. NPM is a reliable package manager which comes with a simple CLI (the npm command). The npm command makes it really easy to install third-party packages, share your own code, and reuse your own code. In addition, the NPM registry, where the packages get hosted, has so many options. By “so many”, I mean hundreds of thousands of options of FREE tools that you can just install and use on your Node servers.

The other big thing about Node is that it comes with a reliable module dependency manager (different than NPM). This module dependency manager is also another thing that we did not have in the JavaScript world.

JavaScript today has what’s known as ECMAScript modules, but these modules — despite being officially part of the language — are really still a work in progress. They’re still not completely supported by all implementations. ECMAScript modules are experimentally-supported in Node.js as well.

Node’s module dependency manager has been available since Node was released and it opened the door to so much flexibility in how we code JavaScript! It is widely used, even for JavaScript that gets executed in the browser, because NPM has many tools to bridge the gap between modules written in Node and what browsers can work with today.

NPM and Node’s module system together make a big difference when you work with any JavaScript system, not just the JavaScript that you execute on backend servers or web browsers. For example, if you have a fancy fridge monitor that happens to run on JavaScript, you can use Node and NPM for the tools to package, organize, and manage dependencies, and then bundle your code, and ship it to your fridge!

3. One Language to Rule Them All

By using Node, you’re committing to the flexible JavaScript language, which is used on every website today. It is the most popular programming language and that statement will continue to be true for decades to come. Despite its problems, JavaScript is actually a good language today.

With Node, you get to have a single language across the full-stack. You use JavaScript in the browser and you use it for the backend as well. There are some subtle but great benefits to that:

  • One language means less syntax to keep in your head, less APIs and tools to work with, and less mistakes over all.

  • One language means better integrations between your frontend code and your backend code. You can actually share code between these 2 sides. For example, you can reuse your frontend UI components code to render server-side HTML.

  • One language means teams can share responsibilities among different projects. Projects don’t need a dedicated team for the frontend and a different team for the backend. You would eliminate some dependencies between teams. The project can be a single team, The JavaScript People. They can develop APIs. They can develop web and network servers. They can develop rich, interactive websites.

There will be dragons!

Node is not all rainbows and unicorns. It has some disadvantages, which are interestingly the same advantage points if you just look at them with different bias. For example, Node’s asynchronous non-blocking nature is just a completely different model of thinking and reasoning about code. If you’ve never done it before, it is going to feel weird at first. You need time to get your head wrapped around this model and to get used to it.

Having a big package registry offering many options means that for every single thing you need to do you have many options to pick from, and some people hate that. You need to constantly research these options and make a mental effort to pick the “better” ones. These options usually have big differences and you might end up spending a lot of time researching them.

Also, NPM along with Node’s module manager enabled shipping smaller and smaller code. This means you need to use more and more packages. It’s not unusual for a Node application to use 300 or more packages. This is both a good thing and a bad thing depending on who you ask. I think it’s a good thing. Smaller packages are easier to control, maintain, and scale, but you do have to make peace with the fact that you’ll be using a lot of them.

Smaller code is actually why Node is named Node! In Node, we build simple small single-process building blocks (nodes) that can be organized with good networking protocols, to have them communicate with each other and scale up to build large, distributed programs.