To upgrade NodeJS can be quite confusing. Do I use apt? Npm? Nvm? For a standard Laravel Forge server, NodeJS is installed with apt. When upgrading NodeJS to a newer version, you will want to upgrade only Node, and not everything on your server (scary!) which some blog posts suggests, hence this post. Just follow the steps below if you're on Forge server and want to upgrade your NodeJS version.
1 - Update NodeJS version in your sources.list to eg. 12.x:
sudo nano /etc/apt/sources.list.d/nodesource.list
2 - Update sources:
sudo apt update
3 - Upgrade (only) NodeJS:
sudo apt install --only-upgrade nodejs
That's it! The last step is actually mentioned in the Laravel Forge Cookbook on how you upgrade NodeJS, but it does not mention steps 1 and 2. If you just run the last command, you probably won't update anything.
UPDATE for Ubuntu 20.04
If you are running Ubuntu 20.04, the above may not do anything. In that case try this:
sudo rm -f /etc/apt/sources.list.d/nodesource* sudo rm -f /etc/apt/keyrings/nodesource.gpg
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt update && sudo apt install --reinstall nodejs -y