On this page
- Why upgrade between LTS lines at all?
- What should you check before touching the server?
- How do you test the new major without risk?
- How do you switch a NodeSource install to the next line?
- How do you switch a dnf module stream?
- Why do native modules need a rebuild and how do you do it?
- How do you restart and verify after the switch?
- How do you roll back if something breaks?
- Questions
Why upgrade between LTS lines at all?
Every Node.js line has a fixed end-of-life date, after which it gets no security fixes. Each even-numbered major is supported for roughly thirty months in total, so an app deployed on one LTS has to move to a newer one at least every couple of years. Patch releases inside a line (24.10 to 24.11, for example) arrive through normal package updates and rarely break anything. Crossing a major is where APIs are removed, the bundled V8 engine changes and compiled add-ons stop loading, so that step deserves a plan.
Go one LTS to the next LTS (22 to 24, then 24 to 26), skipping the odd-numbered releases. Jumping over two lines in one move is possible, but you then debug two sets of changes at once.
What should you check before touching the server?
Start with inventory. Record what runs now, which version each app expects, and which dependencies contain compiled code.
node -v
npm -v
npm ls --depth=0
npm ls --all 2>/dev/null | grep -Ei "node-gyp|bcrypt|sharp|sqlite3|canvas"
find node_modules -name "*.node" | headAny .node file is a native add-on compiled against a specific Node ABI. Next, read the release notes of the target major, especially the list of semver-major changes, and check the engines field of your main frameworks. Update dependencies that announce support for the new line before you switch runtimes; it is easier to separate a library bug from a runtime bug that way.
Finally, make sure you can restore data. If the app has a database on the same instance, take a dump. Daily backups as an add-on and restic are covered in the daily backups tutorial.
How do you test the new major without risk?
Run the app on the target version side by side with the live one. With nvm in your user account you can do that on the same server, on another port:
nvm install 24
cd ~/apps/shop-test
git pull
rm -rf node_modules
nvm exec 24 npm ci
nvm exec 24 npm test
PORT=3101 nvm exec 24 node server.jsHit the test port with curl and walk through the important paths of the app. Watch for deprecation warnings at startup; run with node --trace-deprecation to see where they come from. For bigger apps, deploy a second small instance from the same image, test there, and destroy it afterwards. You pay only for the term you use.
How do you switch a NodeSource install to the next line?
Run the setup script of the new major; it rewrites the apt or dnf source, and the next upgrade pulls the new package. Stop the app first if you want a clean cut, or switch and restart in one short window.
# Ubuntu and Debian
curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y nodejs
node -v
# Rocky and Alma
curl -fsSL https://rpm.nodesource.com/setup_24.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo dnf install -y nodejs --allowerasing
node -vBefore you start, note the exact installed version with apt policy nodejs or dnf list installed nodejs. That string is what you need for a rollback.
How do you switch a dnf module stream?
On Rocky Linux and AlmaLinux 9 with the AppStream packages, reset the current stream, enable the new one and sync installed packages to it:
dnf module list nodejs
sudo dnf module reset -y nodejs
sudo dnf module enable -y nodejs:24
sudo dnf distro-sync -y
node -vIf the stream you want is not listed, your point release does not ship it yet. Use the NodeSource repo instead, after resetting the module so its filtering does not hide the third-party package.
Why do native modules need a rebuild and how do you do it?
Compiled add-ons are built against the module ABI of one Node.js major. After the switch they fail with an error mentioning NODE_MODULE_VERSION. The fix is to rebuild or reinstall them on the new runtime. Make sure a compiler is present, then reinstall from the lockfile:
# toolchain, once
sudo apt install -y build-essential python3 # Ubuntu, Debian
sudo dnf install -y gcc-c++ make python3 # Rocky, Alma
cd /home/deploy/apps/shop
rm -rf node_modules
npm ci
npm rebuildnpm ci installs exactly what package-lock.json lists and runs install scripts, so prebuilt binaries for the new ABI are downloaded where they exist and the rest compiles locally. Global tools installed with npm -g need the same treatment: reinstall PM2 and others after the switch, then run pm2 update so the daemon itself moves to the new runtime, as explained on the PM2 page.
How do you restart and verify after the switch?
Restart the service, check that it stays up, and compare behaviour with your test run.
sudo systemctl restart shop
systemctl status shop --no-pager
journalctl -u shop -n 50 --no-pager
curl -fsS http://127.0.0.1:3000/healthKeep an eye on memory and CPU for the first hours. A new V8 can change garbage collection patterns; if memory creeps up, compare ps -o rss,cmd -C node with your old figures before blaming the app. The usage monitoring tutorial shows how to watch the instance from the shell.
How do you roll back if something breaks?
Reinstall the previous line and the previous dependency tree, then restart. With NodeSource, run the old setup script and install the exact version you noted:
curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
sudo -E bash nodesource_setup.sh
sudo apt install -y --allow-downgrades nodejs=22.21.0-1nodesource1
cd /home/deploy/apps/shop
git checkout HEAD~1 -- package-lock.json
rm -rf node_modules
npm ci
sudo systemctl restart shopUse the version string from your own notes; the one above is only an example of the format. On Rocky or Alma with streams, reset and enable the old stream, then run sudo dnf distro-sync -y. If you used nvm, a rollback is a single edit of the binary path in the unit file. After rolling back, keep the failing log lines: they usually name the dependency that needs an update.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Upgrade Node.js safely
Can I skip a major, for example 20 straight to 24?
You can, and many apps survive it, but you inherit two releases of breaking changes at once. Testing one line at a time makes failures easier to trace.
Will apt upgrade move me to a new major by itself?
Not with a numbered NodeSource source such as setup_22.x. With setup_lts.x, rerunning the script after a new LTS appears does switch the line, which is why pinning a number suits production.
What does the NODE_MODULE_VERSION error mean?
A compiled add-on was built for a different Node.js major. Delete node_modules and run npm ci on the new runtime so it is rebuilt or a matching prebuilt binary is fetched.
Should I upgrade npm separately?
Normally no. Each Node.js release ships with a tested npm. Upgrade npm alone only when a project explicitly needs a newer one.
How long does an upgrade take?
The package switch takes a minute. Dependency reinstall depends on the project size. Plan a short maintenance window and test beforehand so the live switch is routine.