JavaScript

How to write and run your first Node.js program on Ubuntu

Node.js is a cross-platform and open-source JavaScript runtime environment. You can utilize Node.js to develop scalable and robust applications. It is an event-driven and lightweight environment that can handle multiple concurrent connections on a single process with minimum overhead. It is also used in chat and messaging, data streaming, I/O bound, and JSON APT-based applications.

This write-up will demonstrate the procedure to write and run your first Node.js program on Ubuntu. First and foremost, you must install the Node.js and npm package on your Ubuntu system by following the below-given section.

How to install Node.js on Ubuntu

Node.js is based on the v8 JavaScript engine of Chrome and can be easily installed on various operating systems such as Windows, macOS, and Linux-based systems e.g. Ubuntu. Each Module in Node.js can be grouped into a single package.

Before installing Node.js, you have to make sure that existing packages of your system are updated. For this purpose, press “CTRL+ALT+T” to open the terminal and then execute the following command in it:

$ sudo apt-get update

Now, write out the below-given command for installing Node.js on your Ubuntu system:

$ sudo apt install nodejs

Enter “Y” to permit the Node.js installation procedure to continue:

The above-given error-free output signifies that we have successfully installed Node.js on our system. For the verification purpose, we will execute the “node” command with the “-v” option:

$ node -v

Here, “v10.19.0” represents the version number of our Node.js:

Lastly, utilize the given command for installing “npm,” which is the package manager of the Node.js:

$ sudo apt install npm

Wait for a few minutes, as “npm” will take some time to complete the installation process:

Type “y” and press Enter to grant permission:

At this point, Node.js and npm are installed successfully, and we are all ready to write our first Node.js program on our Ubuntu system.

How to write your first Node.js program on Ubuntu

To write your first Node.js program on a Ubuntu system, firstly create a new JavaScript file with the extension “js” and open it up in any command-line editor. In our case, we will utilize the “nano” text editor and create our “hello.js” JavaScript file:

$ nano hello.js

The created “hello.js” JavaScript file will be opened in the nano text editor:

As a beginner, you must step into Node.js programming slowly and gradually. That’s why we will keep our first Node.js program simple.

Now, in the opened “hello.js” file, we will add the below-given statement and press “CTRL+S” to save it:

console.log("This is linuxhint.com");

In Node.js, the “console” is an object and the “log()” method with the “console” object will print out our added “This is linuxhint.com” string on the terminal:

After saving the added code, press “CTRl+X” to exit from the opened nano text editor.

How to run your first Node.js program on Ubuntu

To check the output of a Node.js program, you have to run it with the help of the “node” command. For instance, we have written a “hello.js” Node.js program in the previous section. Now, to execute it, we will specify the file name “hello.js” in the “node” command and hit “Enter”:

$ node hello.js

From the above-given image, you can see that the execution of our “hello.js” Node.js program outputs the string “This is linuxhint.com”, added in the “console.log()” method.

Conclusion

To write and run your first Node.js program in Ubuntu, firstly install node.js and NPM(Node Package Manager) using “sudo apt install nodejs” and “sudo apt install npm” commands respectively. After installation, create a file with the “.js” extension, write Node.js code in it, and execute the file using the “node filename” command. This write-up explained the procedure to write and run your first Node.js program on Ubuntu with a step-by-step guide.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.