Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
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.
| Item | Value |
|---|---|
| Programs | node, npm, npx (corepack for yarn/pnpm) |
| RHEL packages | nodejs (AppStream, module streams on RHEL 8/9) |
| Ubuntu packages | nodejs from NodeSource (Ubuntu's own version is old) |
| Project files | package.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 |
| Network | registry.npmjs.org (443) |
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 --versionOn 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.
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.0Op het werk
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.
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-updates2. A company registry or mirror (Nexus, Artifactory, GitHub Packages):
registry=https://nexus.example.com/repository/npm/
always-auth=true
engine-strict=trueThe 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:
{
"name": "shop-api",
"version": "1.4.0",
"engines": { "node": ">=22 <25" },
"scripts": {
"start": "node server.js",
"test": "node --test",
"build": "tsc -p ."
}
}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[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.targetsystemctl daemon-reload
systemctl enable --now shop-api
journalctl -u shop-api -f
curl -s http://127.0.0.1:3000/healthThen the Nginx reverse proxy to 127.0.0.1:3000 (see the Nginx guide). Secrets go into /etc/shop-api.env with rights 600.
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-runOnthoud
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:
npxruns a tool once without a global install.
Make it safe
Security checklist:
- Only LTS versions; update Node for security releases.
- Commit
package-lock.json;npm ciin 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-scriptsin 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.1behind Nginx; secrets in env files with600.
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.listTest the app on the new major version before you upgrade production.
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
EACCES: permission denied with npm install -g | Global folder belongs to root. | Use the ~/.npm-global prefix (above); never sudo npm. |
node: command not found after install with nvm | The 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 sync | package.json was changed without npm install. | Run npm install locally and commit the new lock file. |
gyp ERR! when installing | Native modules need build tools. | dnf install gcc-c++ make python3 / apt install build-essential python3. |
ENOSPC: System limit for number of file watchers reached | Too few inotify watchers (dev servers). | sysctl -w fs.inotify.max_user_watches=524288 (and in /etc/sysctl.d/). |
EADDRINUSE: address already in use :::3000 | Another process uses the port. | ss -ltnp 'sport = :3000'; change PORT. |
Next steps
- Command page: npm — every option and example.
- npm in DevOps pipelines: npm — Node.js packages and scripts.
- The proxy in front: Install and set up Nginx. In containers: Install and set up Docker Engine.
- Scan the dependencies: Command page: trivy.
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.