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 InterpreterGreater-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 CodeWhy 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.”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 ScriptTo run the script, use the node command and specify the script’s path in the terminal.
$ node script.js
10
Node Js ScriptFinal 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.
Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.