Skip to content

Installing Node.js

Before getting started with Node.js, you'll need to download and install it on your computer. Here are the steps to do that:

  • Go to the official Node.js website (https://nodejs.org).
  • Download the appropriate installer for your operating system.
  • Run the installer and follow the prompts to install Node.js on your computer.

In Ubuntu 22.04, we can install the packages provided by Nodesource (https://github.com/nodesource/distributions#deb).

Long Term Support versions

As a general rule we will work with LTS versions so that this versions have a longer lifecycle.

Verifying the installation

After installing Node.js, you can verify that it's working correctly by opening a terminal or command prompt and running the following command:

node -v

This should display the version number of Node.js that you have installed.

Using the console and the REPL interpreter

Once you've verified that Node.js is installed correctly, you can start using it by opening a terminal or command prompt and running the node command. This will open the Node.js console, which allows you to run JavaScript code interactively.

You can also use the REPL (Read-Eval-Print Loop) interpreter, which is a tool that allows you to enter JavaScript code and see the results immediately. To start the REPL interpreter, open a terminal or command prompt and run the node command without any arguments.

Here's an example of how to use the REPL interpreter:

> let x = 10
undefined
> let y = 5
undefined
> x + y
15
> console.log("Hello, world!")
Hello, world!
undefined

To exit the REPL, type .exit or press Ctrl+C twice.

In this example, we defined two variables (x and y) and then added them together. We also used the console.log() method to print a message to the console.

Using the console and the REPL interpreter can be a helpful way to experiment with Node.js and learn how to write JavaScript code for the server-side.

Creating our first Node.s app

To create a Node.js program that takes in the user's name as a command-line argument and outputs a greeting to the console the program should:

  1. Use the process.argv array to get the user's name from the command line.
  2. Output a greeting to the console in the following format: Hello, <username>!, where <username> is the input value.

Here's an example of how your program should behave:

$node hello.js Alice
Hello, Alice!

The code should be something like that:

// hello.js

// Get the command-line arguments
const args = process.argv;

// Get the username from the command-line arguments
const username = args[2];

// Print out the greeting
console.log(`Hello, ${username}!`);

More resources