How to install Node.js & NPM using yum on Amazon Linux?

How can I use yum install nodejs to set up Node.js and NPM on Amazon Linux?

I’ve seen guides that involve manually installing dependencies and compiling Node.js from source, but I feel like Node.js and NPM should be available in a public repository for easier installation.

Is there a way to install both Node.js and NPM in a single command using yum on AWS Amazon Linux?

I’ve worked with Amazon Linux for a while, and the easiest way to get Node.js and NPM up and running is through the amazon-linux-extras repository. No hassle, just a few commands!

:one: First, check if Node.js is available:

amazon-linux-extras list | grep nodejs

This will show available versions, such as nodejs16 or nodejs18.

:two: Enable the required Node.js version:

sudo amazon-linux-extras enable nodejs18

:three: Now, install Node.js and NPM:

sudo yum install -y nodejs

:four: Verify the installation:

node -v
npm -v

And that’s it! With yum install nodejs, you get Node.js and NPM installed in minutes—no need to build anything manually. :rocket:

@emma-crepeau method works great, but if you need a newer or specific version of Node.js that isn’t available in amazon-linux-extras, you should use NodeSource. It’s my go-to for flexibility!"*

:one: First, install NodeSource’s repository setup script:

(Replace 18.x with 20.x if you need Node.js 20.)

:two: Now, install Node.js and NPM:

sudo yum install -y nodejs

:three: Verify the installation:

node -v
npm -v

This method ensures you get the latest stable version straight from the source. If amazon-linux-extras doesn’t have what you need, this is the way to go! :fire:

@joe-elmoufak method is solid for getting the latest Node.js, but what if you need multiple Node.js versions on the same system? That’s where NVM (Node Version Manager) is a lifesaver!"*

:one: First, install NVM:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

(Replace 0.39.5 with the latest version from NVM’s official repo.)

:two: Reload your terminal session:

source ~/.bashrc

:three: Install the latest LTS version of Node.js:

nvm install --lts

:four: Set the installed version as default:

nvm use --lts

:five: Verify the installation:

node -v
npm -v

The biggest advantage? You can easily switch between Node.js versions—perfect for working on multiple projects! If you’re frequently testing across different environments, NVM is the best way to manage Node.js. :raised_hands: