Skip to content

Node.js


Node.js is an open-source, cross-platform JavaScript runtime environment that enables JavaScript execution on the server side.

As system administrators, we may not write Node.js applications daily, but we frequently encounter them in various scenarios—whether running web applications, managing microservices, or troubleshooting performance issues.

I used Debian and Ubuntu server editions, namely Debian 12 & 13, Ubuntu 22.04 & 24.04 LTS Servers.



Several installation methods exist, but we’ll focus on the two most common approaches.

1.1. Install System Packages (System-wide)

Section titled “1.1. Install System Packages (System-wide)”

Installing distribution packages provides a stable but potentially older version of Node.js. This method installs Node.js system-wide, accessible to all users. Ideal for production environments.

Update package repositories:

Terminal window
sudo apt update

Install Node.js and NPM (Node Package Manager):

Terminal window
sudo apt install -y nodejs npm

Verify installation:

Terminal window
node -v
npm -v

1.2. Install Using NVM (Node Version Manager) - User-based

Section titled “1.2. Install Using NVM (Node Version Manager) - User-based”

NVM allows installation of the latest Node.js versions on a per-user basis. This provides access to newer features while maintaining isolation from system packages. Ideal for development and testing.

Install curl (Debian 13 doesn’t include it by default):

Terminal window
sudo apt update
sudo apt install -y curl

Download and install NVM (Node Version Manager):

Terminal window
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash

Either restart your shell or source the NVM script:

Terminal window
\. "$HOME/.nvm/nvm.sh"

Install the latest Node.js LTS version (22.x as of writing):

Terminal window
nvm install 22

Verify the installation:

Terminal window
node -v

Check the active Node.js version:

Terminal window
nvm current

Verify NPM version:

Terminal window
npm -v

Install another version (e.g., current release 23.x):

Terminal window
nvm install 23

Check current active version:

Terminal window
nvm current

Switch to version 22:

Terminal window
nvm use 22

List installed versions:

Terminal window
nvm ls

Set version 22 as the default:

Terminal window
nvm alias default 22

Remove version 23

Terminal window
nvm uninstall 23


Create a simple JavaScript program:

Terminal window
nano hello.js

Fill as below:

console.log("Hello from Node.js!");

Execute the program:

Terminal window
node hello.js

Create a more practical example:

Terminal window
nano time.js

Fill as below:

const now = new Date();
console.log(`Time now is: ${now.toLocaleString()}`);

Run the program:

Terminal window
node time.js

You can install/run packages from https://www.npmjs.org/

Run a package without permanently installing. It asks to install, you can say Y.

Terminal window
npm exec cowsay "Hello world"

Install a package locally, we’ll use it in a script

Terminal window
npm install chalk@4

Saved to ./node_modules/

Let’s use it in a script

Terminal window
nano chalktest.js

Fill as below:

const chalk = require('chalk');
console.log(chalk.green('Success message'));
console.log(chalk.red.bold('Error message'));
console.log(chalk.blue.underline('Info message'));
console.log(chalk.yellow('Warning message'));

Run it:

Terminal window
node chalktest.js

Update installed packages:

Terminal window
npm update

List locally installed packages:

Terminal window
npm list

View specific package details:

Terminal window
npm list chalk

Check for outdated packages:

Terminal window
npm outdated

If you want to uninstall a package:

Terminal window
npm uninstall chalk