Skip to main content

NodeJS 101

·389 words·2 mins
xx
Posts 101 nodejs ubuntu
Table of Contents
Node.js is designed to build scalable network applications as an asynchronous event-driven Javascript runtime.

NodeJS Releases
#

Same as others, Node.js has 3 major releases including Current, LTS, and End-of-Life. And Node.js has taken the approach to make all its LTS to have even-numbered releases.

For example, as of today, maintenance LTS is at v20.x, and active LTS is at v22.x. Older LTS (EOL) versions include v12.x, v16.x, v18.x.

See Node.js Releases for more details.

Ubuntu’s default repositories include Node.js (v12.22.9 as of today), offering a consistent experience across systems. While not the latest, this version is stable and suitable for quick experimentation with Node.js.

Default repositories always prioritize stability over the latest features. This makes then often lag behind the current Node.js releases (v22.x).

Because of that, I choose to install Node.js via NodeSource PPA1 instead of default repositories.

Quickstart
#

PPAs have more versions of Node.js available than the official Ubuntu repositories. In this case, I install Node.js v20.x in my Ubuntu OS for some experiments.

First, I download the setup script (nodesource_setup.sh).

Refer to the NodeSource documentation for more information on the available versions.

Then, I just run the setup script with sudo.

xx@pf22:~$ curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
xx@pf22:~$ sudo apt install -y nodejs

Next, just run the commands to verify the installation.

xx@pf22:~$ node -v
v20.19.3
xx@pf22:~$ npm -v
10.8.2
xx@pf22:~$ npx -v
10.8.2

Quick Uses
#

Here, I’ll show 2 use cases here.

First, I’ll install a web server http-server with npm. Then follow by running it with npx.

xx@pf22:~$ npm i http-server

added 48 packages in 2s

15 packages are looking for funding
  run `npm fund` for details
xx@pf22:~$ npx http-server
Starting up http-server, serving ./

http-server version: 14.1.1

http-server settings:
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: false
Default File Extension: none

Available on:
  http://127.0.0.1:8080
  http://10.255.255.254:8080
  http://172.20.124.23:8080
Hit CTRL-C to stop the server

In second example, I’ll run (with npx) a package without local installation.

xx@pf22:~$ npx -y cowsay "Ubuntu Shell"
 ______________
< Ubuntu Shell >
 --------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
xx@pf22:~$

Links#


  1. Personal Package Archive (PPA): It’s a software repository that allows developers to distribute software packages outside of the official repositories. ↩︎

Author
xx
cli-geek

Related

Adding Python3.11
·261 words·2 mins
Posts 101 python ubuntu
Steps to install alternate Python version for Noble
Mistake in List Iteration
·169 words·1 min
Posts 101 python
Unexpected behaviour in your Python loops.
Migration in 2024
·23 words·1 min
xx
Posts ubuntu
Quick migration from WSL to Ubuntu.