Advanced
Searching inInstall guides
How to search
  • Paste an error message in quotes, e.g. "permission denied", to find the fix table that explains it.
  • Choose an area and a part of the guide (install, first settings, daily use, security, fixes) to go straight to the right step.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Guide 19Node.js · Languages and build

Install and set up Node.js and npm

Node.js runs JavaScript on servers. Many web apps, APIs and front-end builds need it, and many DevOps tools are installed with npm. As an engineer you install the right LTS version, run builds in CI with npm ci, and run Node apps safely in production under systemd behind Nginx. This guide shows all of that on RHEL and Ubuntu.

  • 10 Lessons
  • 17 Commands

$ run as a normal user# run as root (or with sudo)

19.1

Know it before you install

node runs the code. npm installs packages from the npm registry into node_modules/ of the project, as written in package.json and pinned in package-lock.json. Use only LTS versions (even numbers: 22, 24) on servers.

ItemValue
Programsnode, npm, npx (corepack for yarn/pnpm)
RHEL packagesnodejs (AppStream, module streams on RHEL 8/9)
Ubuntu packagesnodejs from NodeSource (Ubuntu's own version is old)
Project filespackage.json, package-lock.json (commit it), node_modules/ (never commit)
npm settings~/.npmrc (user), .npmrc (project)
Global packages/usr/lib/node_modules or ~/.npm-global
Networkregistry.npmjs.org (443)

Commands in this lesson

19.2

Install on RHEL, Rocky, Alma and Fedora

List the versions, choose an LTS stream, install:

dnf module list nodejs
dnf -y module enable nodejs:22
dnf -y install nodejs npm
node --version
v22.20.0
npm --version

On RHEL 10 and Fedora just dnf -y install nodejs. For a version that RHEL does not have yet, use NodeSource: curl -fsSL https://rpm.nodesource.com/setup_24.x | bash - then dnf -y install nodejs.

19.3

Install on Ubuntu and Debian

The NodeSource repository gives the current LTS lines:

apt-get update
apt-get install -y ca-certificates curl
curl -fsSL https://deb.nodesource.com/setup_24.x -o /tmp/nodesource_setup.sh
less /tmp/nodesource_setup.sh
bash /tmp/nodesource_setup.sh
apt-get install -y nodejs
node --version
v24.10.0

At work

On a developer laptop use a version manager (nvm, fnm or volta), so each project can use its own Node version. On servers use the package, so updates come with dnf/apt.

19.4

First configuration: the settings to make first

1. Global tools without sudo (on your own user):

mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
echo 'export PATH=$HOME/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
npm install -g npm-check-updates

2. A company registry or mirror (Nexus, Artifactory, GitHub Packages):

.npmrc
registry=https://nexus.example.com/repository/npm/
always-auth=true
engine-strict=true

The token goes into the user's ~/.npmrc or a CI secret (//nexus.example.com/repository/npm/:_authToken=${NPM_TOKEN}), never into Git.

3. Pin the Node version in the project, so CI and servers use the same:

package.json
{
  "name": "shop-api",
  "version": "1.4.0",
  "engines": { "node": ">=22 <25" },
  "scripts": {
    "start": "node server.js",
    "test": "node --test",
    "build": "tsc -p ."
  }
}

19.5

Run a Node app in production

Run it as its own user, under systemd, listening only on localhost — Nginx is the public front:

useradd --system --create-home --home-dir /opt/shop-api --shell /sbin/nologin shopapi
cd /opt/shop-api && sudo -u shopapi npm ci --omit=dev
/etc/systemd/system/shop-api.service
[Unit]
Description=Shop API (Node.js)
After=network.target
 
[Service]
User=shopapi
WorkingDirectory=/opt/shop-api
Environment=NODE_ENV=production
Environment=PORT=3000
Environment=HOST=127.0.0.1
EnvironmentFile=-/etc/shop-api.env
ExecStart=/usr/bin/node server.js
Restart=on-failure
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/opt/shop-api/tmp
 
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now shop-api
journalctl -u shop-api -f
curl -s http://127.0.0.1:3000/health

Then the Nginx reverse proxy to 127.0.0.1:3000 (see the Nginx guide). Secrets go into /etc/shop-api.env with rights 600.

19.6

Daily use: the commands and the real uses

npm ci
npm install express@5
npm install --save-dev typescript
npm run build
npm test
npm outdated
npm audit
npm ls express
npx eslint .
npm pack --dry-run

Remember

In CI use **npm ci**, not npm install. npm ci installs exactly what package-lock.json says, removes old node_modules first, and fails if the lock file does not match.

Real uses at work:

  • Build front-ends (React, Vue, Angular) in CI and ship the static files to Nginx or S3.
  • Run APIs and workers under systemd or in containers (node:24-alpine).
  • Install tools: npx runs a tool once without a global install.

Commands in this lesson

19.7

Make it safe

Security checklist:

  • Only LTS versions; update Node for security releases.
  • Commit package-lock.json; npm ci in CI.
  • npm audit (and Trivy or Dependabot) in the pipeline; fix high and critical issues.
  • Install scripts of packages run code on your machine: npm ci --ignore-scripts in CI when the project allows it.
  • Check new package names carefully (typosquatting); prefer well-known packages.
  • Never run Node apps as root; listen on 127.0.0.1 behind Nginx; secrets in env files with 600.

Commands in this lesson

19.8

Update and remove

dnf -y upgrade nodejs
apt-get update && apt-get install --only-upgrade -y nodejs
dnf -y module reset nodejs && dnf -y module enable nodejs:24 && dnf -y distro-sync
dnf -y remove nodejs
apt-get purge -y nodejs && rm -f /etc/apt/sources.list.d/nodesource.list

Test the app on the new major version before you upgrade production.

Commands in this lesson

19.9

Fix common problems

ProblemCauseFix
EACCES: permission denied with npm install -gGlobal folder belongs to root.Use the ~/.npm-global prefix (above); never sudo npm.
node: command not found after install with nvmThe shell was not reloaded.source ~/.bashrc; nvm use 22.
Old Node version on Ubuntu (v18 or older)Ubuntu's own package.Install from NodeSource (above).
npm ci fails: lock file out of syncpackage.json was changed without npm install.Run npm install locally and commit the new lock file.
gyp ERR! when installingNative modules need build tools.dnf install gcc-c++ make python3 / apt install build-essential python3.
ENOSPC: System limit for number of file watchers reachedToo few inotify watchers (dev servers).sysctl -w fs.inotify.max_user_watches=524288 (and in /etc/sysctl.d/).
EADDRINUSE: address already in use :::3000Another process uses the port.ss -ltnp 'sport = :3000'; change PORT.

19.10

Next steps

1Which command do you use in CI: npm install or npm ci? Why?

npm ci. It installs exactly the versions in package-lock.json and fails if the files do not match.

2Why should a Node app listen on 127.0.0.1 and not on all addresses?

Only Nginx (with HTTPS and limits) should be public; the app is not reachable directly.

All commands in this chapter