How to Install and Run Node Js in Linux

In this guide, we will install the latest version of Node Js and NPM in Ubuntu, RedHat, CentOS, Manjaro and run the first application in Linux.

Node Js is not a programming language but a javascript runtime for backend development, developed in chrome v8 javascript engine. To make development easier for the web developer who wants to have one skillset for frontend and backend.

NPM is a package manager for Node Js that helps to install modules within the terminal. Mostly used to discover, install, publish modules, just like a PIP for Python.

Step 1: Install Node Js in Linux

Node Js and NPM are ready to install from your default Linux Repository without any extra steps. Before installation check, if it’s already installed in your system or not.

$ node -v #To check Nodejs is installed
$ npm -v #To check NPM is installed

Jump to the second step if it’s already installed.

Ubuntu/Debian

$ sudo apt install nodejs 
$ sudo apt install npm

RedHat/CentOS/Fedora

$ dnf install nodejs
$ dnf install npm

Arch/Manjaro

$ pacman -Sy nodejs
$ pacman -Sy npm

Once Node Js and NPM are installed in your Linux system, you can run any javascript application using a terminal or writing a separate script.

Step 2: Run Node Js using Terminal

Open your terminal using Ctrl+Alt+T and type the below command, to enter into Node Js console.

$ node
>
Running Node Js Interpreter
Running Node Js Interpreter

Greater-than symbol will start to appear once you enter the Node Js console. Copy or write the below code.

> console.log("Hello, TREND OCEANS")
Hello, TREND OCEANS
undefined

If you look at the end of the line, there is an undefined output. What’s that?

First Node Js Code
First Node Js Code

Why Node Js displays “Undefined” in the Console

By default, if you do not specify anything in return. Node Js will output “undefined” in the end. To avoid it, you can specify anything in return.

> console.log("Hello, TREND OCEANS") || 0; 
Hello, TREND OCEANS
0

Above I have specified 0 in return which replaces undefined.

Replacing default "undefined"
Replacing default “undefined.”

Step 3: Run Node Js using Script

Having a separate script with javascript functions and operator is more convenient than using the Node Js console.

// Simple Addition Function in Javascript
function add(a, b) {
return a+b
}
console.log(add(4, 6))

Write your code in your favorite Text Editor like Visual Studio Code and save your file using “.js” as an extension.

Writing Node Js Script
Writing Node Js Script

To run the script, use the node command and specify the script’s path in the terminal.

$ node script.js
10
Node Js Script
Node Js Script

Final Thought

Node Js is a popular and useful tool for developing scalable and fast web applications. If you found any difficulty while installing or running, let us know in the comment section.

This Post Has 2 Comments

  1. James

    Really helped me alot and yes finally installed it.

Leave a Reply