How to Install Node.js on Windows, Mac, and Linux
This tutorial will show you how to install Node.js correctly, or fix your current Node.js installation if it isn’t installed correctly. We’ll use Node Version Manager (NVM) so that you can install multiple versions of Node.js on your computer at the same time.
How to Tell if Node.js Is Installed
To see if Node.js is installed, type this command in a terminal:
node -v
If node is installed, it will print out the version number, which might be something like v18.0.0 (at the time of writing this).
A Note on sudo
You should NOT use sudo in front of your npm commands.
If a tutorial tells you to use the keyword sudo in front of your npm commands, be careful, because it means that you are opening up your computer to attacks.
The security problem is that if you use sudo in front on npm, it can give unknown scripts full access to your machine by letting them run with root permissions.
Try omitting the word sudo in your npm commands. If you get an error about permission problems, it means Node.js isn’t installed correctly.
If you want to test it, run this command to see if there is a permissions problem:
npm install -g prettier
If you get a permissions error (something like EACCES), I would recommend uninstalling your current version of Node.js and reinstalling it using NVM as explained below. There are multiple solutions, but I think the NVM way is a good choice, because it is easy to set up, and it lets you run multiple versions of Node.js on your computer at the same time.
Use NVM to Install Node.js
The best way I’ve found to install Node.js is to use Node Version Manager or nvm.
Use one of these links to see the most current install instructions:
After you have NVM installed, open a new terminal window, and install the current LTS (Long-Term Support) version of Node.js.
You can find the current LTS version of Node.js by looking in the LTS column of the table on this page. At the time of writing this post, the current LTS version is 18 (only the first number matters for nvm).
After you know the current LTS version, use it in the following command, replacing 18 with the version that you want to install:
nvm install 18
As soon as the command finishes, you should be able to check the Node.js version like this:
node -v
If you want to run multiple versions at the same time, you can install other versions in the same way:
nvm install 16
Then set the version you want to use:
nvm use 16
See the nvm docs for more details.
nvm vs. yarn
There are two common ways to install Node.js packages: nvm and yarn.
nvm comes with Node.js, so it’s already installed.
yarn can be installed like this:
npm i -g yarn
It doesn’t matter which one you choose, but be consistent throughout a project. If you start a project with npm don’t switch to yarn (or vice versa) later in that project unless you know what you’re doing.
Managing Packages with npm
Here’s a short cheatsheet for npm.
Initialize a new Node.js project:
npm init
Install a package into a project:
npm install lodash
Install a package into a project as a development dependency:
npm install -D prettier
Install a package globally:
npm install -g prettier
Run a custom script that is listed in in package.json:
npm run my-custom-script
Read more in the npm docs.
Managing Packages with yarn
Here’s a short cheatsheet for yarn.
Initialize a new Node.js project:
yarn init
Install a package into a project:
yarn add lodash
Install a package into a project as a development dependency:
yarn add -D prettier
Install a package globally:
yarn add -g prettier
Run a custom script that is listed in in package.json:
yarn my-custom-script
Read more in the yarn docs.
If you run into any problems, leave a comment below.