What Exactly is Node.js? Explained for Beginners

As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. In the following “hello world” example, many connections can be handled concurrently. Upon each connection, the callback is fired, but if there is no work to be done, Node.js will sleep.

CJS

const { createServer } = require('node:http');
const hostname = '127.0.0.1';
const port = 3000;
const server = createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

MJS

import { createServer } from 'node:http';
const hostname = '127.0.0.1';
const port = 3000;
const server = createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

This is in contrast to today’s more common concurrency model, in which OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. Furthermore, users of Node.js are free from worries of dead-locking the process, since there are no locks. Almost no function in Node.js directly performs I/O, so the process never blocks except when the I/O is performed using synchronous methods of Node.js standard library. Because nothing blocks, scalable systems are very reasonable to develop in Node.js.

What is Node.js?

“Node.js is an open-source and cross-platform JavaScript runtime environment.” – Nodejs.dev Docs

This sounds like a cool, straightforward answer. But for a beginner, this definition might raise further questions. So let’s break it down and understand what it means.

Node.js is open-source: This means that the source code for Node.js is publicly available. And it’s maintained by contributors from all over the world. The Node.js contribution guide shows you how to contribute.

Node.js is cross-platform: Node.js is not dependent on any operating system software. It can work on Linux, macOS, or Windows.

Node.js is a JavaScript runtime environment: When you write JavaScript code in your text editor, that code cannot perform any task unless you execute (or run) it. And to run your code, you need a runtime environment.

Browsers like Chrome and Firefox have runtime environments. That is why they can run JavaScript code. Before Node.js was created, JavaScript could only run in a browser. And it was used to build only front-end applications.

Node.js provides a runtime environment outside of the browser. It’s also built on the Chrome V8 JavaScript engine. This makes it possible to build back-end applications using the same JavaScript programming language you may be familiar with.

Differences Between the Browser and Node.js Runtime Environments

Both the browser and Node.js are capable of executing JavaScript programs. But there are some key differences that you need to know. They include the following.

Access to the DOM APIs

With the browser runtime, you can access the Document Object Model (DOM). And you can perform all the DOM operations. But Node.js does not have access to the DOM.

Node.js exposes almost all the system resources to your programs. This means you can interact with the operating system, access the file systems, and read and write to the files. But, you do not have access to operating systems and file systems from the browser.

Window vs Global object

JavaScript has a built-in global object. The JavaScript global object for the browser is called the window object. In Node.js, the global object goes by the name global.

The window object contains methods and properties available only in the browser environment.

Control over runtime versions

With Node.js, you can choose which version to run your server-side application on. As a result, you can use modern JavaScript features without worrying about any version-specific inconsistencies.

Contrast this to the browser runtime environment. As a developer, you have no control over the version of browsers your clients use to access your app.

Loading modules (import vs require keywords)

Node.js offers out-of-the-box support for CommonJS and ES modules. You can load modules using the require keyword (CommonJS syntax) and the import keyword (ES syntax).

Some modern browsers support ES modules. This means you can use import ES modules. But you will still need to create bundles to cater to older browsers that do not support ES modules.

How Much JavaScript Do You Need to Get Started with Node?

If you are an absolute beginner to JavaScript, I recommend that you start with the basics.

Become familiar with basic JavaScript concepts first. Then, you can move on to learning to build server-side applications with Node.js.

There’s no way you’ll ever exhaust all there is to learn about JavaScript. So, how to determine when you know enough JavaScript to get started with Node.js?

The Nodejs.dev documentation provides a list of JavaScript topics to learn before diving deep with Node.js.

Once you have a grasp of JavaScript basics, then you can get started with Node.js

How to Get Started with Node.js

Let’s see how you can create your first Node.js application. This section will show you how to run Node.js scripts from the command line.

How to download and install Node.js

First, you need to download and install Node.js. There are different ways you can do that. If you are a beginner, I would suggest that you download Node.js from the official website.

The official Node.js website

Official packages are available on the website for all major platforms (Windows, macOS, and Linux). Download and install the appropriate package for your system.

How to check the Node.js version

To check the Node.js version, run the command node --version in your terminal.
If the installation was successful, you will see the version of Node.js you installed. You should get a response like the screenshot below.

How to check the Node.js version

How to run Node.js from the command line

Let’s build a simple Hello World app.

Create a new project folder. You can call it my-project. Open the project in your code editor. Inside the folder, create an app.js file.

Add the following code to app.js

Hello world NodeJs

As you can see, this is JavaScript code.

You can run the script in the command line by running the command node <fileName>. In this case, the file name is app.js.

Run the following command in your terminal to execute the Hello world program:

node app.js

You should see the string “Hello world.” logged in your terminal like so.

Congratulations! You just ran your first Node.js application.

Should You Learn Node.js?

Here are some reasons why you should consider learning Node.js

Node.js allows you to write JavaScript on both client and server.

One of the advantages of Node.js is that it allows you to work on both the front-end and back-end of your application. And you use one programming language – JavaScript – to do so.

This is good news for front-end developers who work with JavaScript. If you want to start working on the server side, it’s easier compared to learning a new back-end language from scratch.

Node has a vibrant community.

As I mentioned earlier in the article, Node.js is open-sourced. It is actively maintained by developers from all over the world.

There is a vibrant community surrounding Node.js. You can find excellent tutorials and solutions to problems when you get stuck.

Node is built on top of Google Chrome’s V8 engine.

Node.js is built on top of the Chrome V8 JavaScript engine. This is significant because the V8 engine powers some of Google’s in-browser applications like Gmail. As such, Google invests heavily to ensure it offers high performance.

Demand in the market

Many big names like Netflix, Uber, Paypal, and LinkedIn, and others use Node.js. Apart from the big names, many startups also use Node.js in developing their applications.

Learning to work with Node.js will make you a desirable candidate in the job market.

The NPM library

The NPM library is one of the excellent resources that comes with Node.js.
The library contains a registry of over a million packages. A package is a reusable piece of code.

You can create a package for a recurring task or problem and share the code with others via the registry.

You can also download packages that others have shared. For many tasks that developers perform regularly, there are packages available for that.

Conclusion

A single blog post like this is not enough to learn all there is to know about Node.js. The purpose of this article was to give you an overview of what Node.js is.

If you were not sure what Node.js is, I hope this article addressed your concerns and cleared your confusion.

Source: Benjamin Semah


How to deploy a NodeJS App with Staas.io

nodejs staas

Start building NodeJS with Staas.io

Node.js is a cross-platform, open-source server environment that can run on Windows, Linux, Unix, macOS, and more. Node.js is a back-end JavaScript runtime environment, runs on the V8 JavaScript engine, and executes JavaScript code outside a web browser.