Skip to content

Instantly share code, notes, and snippets.

@Hri7566
Created September 25, 2023 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hri7566/966e7082d8cd0507beaff6ebc660380b to your computer and use it in GitHub Desktop.
Save Hri7566/966e7082d8cd0507beaff6ebc660380b to your computer and use it in GitHub Desktop.
How to install node on a Linux server without being a dumdum

How to install node on a Linux server without being a dumdum

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.

Install fnm

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.

Install node itself with fnm

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

Install pnpmand pm2

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

How to setup a project (with git, pnpm, and 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

Bringing your project back online if it didn't persist after a server restart

Sometimes, things just don't start right, so we can use pm2 to remember what was running.

$ pm2 resurrect

Stopping your project

$ 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment