I keep having people login to my VPS and use node the wrong way. This document is to stop people from trying to install it with apt and do crazy things with npm that don't make sense.
Do not use the version of node from apt. It is older than the Pyramids of Giza. Instead, use fnm.
$ curl -fsSL https://fnm.vercel.app/install | bash
After installing it, follow the on-screen instructions or log out and back in if you're unsure.
We use fnm to install node. Use this tool if you need to switch versions. This tool specifically manages versions of node for you.
$ fnm ls-remote # find the latest version
$ fnm install 20.7 # install that latest version
Npm and yarn are ancient, we use pnpm this decade. We're also installing pm2 to keep your projects online.
$ npm i -g pnpm pm2
I don't recommend making your project directly in the production environment, but you do you....
When you make a new project, use git. It will make your life a whole lot easier.
$ mkdir project && cd project
~/project$ git init
~/project$ pnpm init
If you already have a project, get its files onto the VPS through whatever method you choose. I usually use git clone, scp, or rsync.
Once your project is setup and you're able to run it with node, use pnpm to keep it online.
~/project$ pm2 start . --name="my-project"
~/project$ pm2 save
Sometimes, things just don't start right, so we can use pm2 to remember what was running.
$ pm2 resurrect
$ pm2 list # Find the project in the list to stop
$ pm2 stop 0 # Tell pm2 to stop that process with its ID or name
$ pm2 save # You can save your stopped projects as well