Node.js
Node.js installation and basic setup
Section titled “Node.js installation and basic setup”0. Specs
Section titled “0. Specs”0.0. The What
Section titled “0.0. The What”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.
0.1. Environment
Section titled “0.1. Environment”I used Debian and Ubuntu server editions, namely Debian 12 & 13, Ubuntu 22.04 & 24.04 LTS Servers.
0.2. Sources
Section titled “0.2. Sources”1. Installation
Section titled “1. Installation”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:
sudo apt updateInstall Node.js and NPM (Node Package Manager):
sudo apt install -y nodejs npmVerify installation:
node -vnpm -v1.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):
sudo apt updatesudo apt install -y curlDownload and install NVM (Node Version Manager):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bashEither restart your shell or source the NVM script:
\. "$HOME/.nvm/nvm.sh"Install the latest Node.js LTS version (22.x as of writing):
nvm install 22Verify the installation:
node -vCheck the active Node.js version:
nvm currentVerify NPM version:
npm -vInstall another version (e.g., current release 23.x):
nvm install 23Check current active version:
nvm currentSwitch to version 22:
nvm use 22List installed versions:
nvm lsSet version 22 as the default:
nvm alias default 22Remove version 23
nvm uninstall 232. Using Node.js
Section titled “2. Using Node.js”2.1. Running Javascript Programs
Section titled “2.1. Running Javascript Programs”Create a simple JavaScript program:
nano hello.jsFill as below:
console.log("Hello from Node.js!");Execute the program:
node hello.jsCreate a more practical example:
nano time.jsFill as below:
const now = new Date();console.log(`Time now is: ${now.toLocaleString()}`);Run the program:
node time.js2.2. Installing/Running Packages
Section titled “2.2. Installing/Running Packages”You can install/run packages from https://www.npmjs.org/
Run a package without permanently installing. It asks to install, you can say Y.
npm exec cowsay "Hello world"Install a package locally, we’ll use it in a script
npm install chalk@4Saved to ./node_modules/
Let’s use it in a script
nano chalktest.jsFill 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:
node chalktest.jsUpdate installed packages:
npm updateList locally installed packages:
npm listView specific package details:
npm list chalkCheck for outdated packages:
npm outdatedIf you want to uninstall a package:
npm uninstall chalk