Installing Node.js and npm for local WordPress development


Node.js is a cross-platformopen-source server environment that can run on most popular operating systems. Node.js lets developers use JavaScript to write command line tools and for server-side scripting. When you install Node.js on your operating system, it comes preinstalled with npm, a command line tool that allows developers to share and use software packages as dependencies in their own projects.

While not specifically a requirement for WordPress development, if you want to develop blocks, contribute to Gutenberg, or use modern tools like wp-env or wp-now, you will need to have Node.js and npm installed on your computer.

In this tutorial, you will learn how to install Node.js and npm on macOS, Ubuntu, and Windows.

Learning outcomes

  1. Why you should install Node.js and npm.
  2. What is required to install Node.js and npm.
  3. How to install Node.js and npm.

Comprehension questions

  1. Are you comfortable installing software on your computer?
  2. Do you have access to a terminal on your computer?
View video transcript

Hey there and welcome to Learn WordPress.
In this tutorial, you’re going to learn how to install Node.js and npm on your computer for local WordPress development.
You will learn why you should install this software,
what the requirements are,
and how to install Node.js and npm on your computer.
Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside of a web browser.
While this might not be specifically applicable to developing with WordPress, Node.js also comes bundled with npm.
npm is both a command line tool and a package manager for JavaScript.
It allows you to install and manage JavaScript packages, also known as dependencies, as well
as run build tools, execute code linters, and automate many other processes during development.
Many WordPress development projects rely on npm, including Gutenberg, the Create Block
tool, the wp-env local WordPress environment, and the new wp-now local WordPress environment.
In order to run Node.js and npm, you’re going to need access to a terminal to run commands.
Depending on your operating system, you might already have an existing terminal installed.
macOS and/or any Linux users can use the terminal that ships with the operating system.
If you are a Windows user and you don’t already have a terminal solution,
we recommend installing and using PowerShell as it’s more beginner-friendly
and more tailored to the Windows operating system. To install PowerShell, either open the
Microsoft Store and search for PowerShell or browse to microsoft.com/powershell. Browse to
Set up and Installation and then follow the instructions to download and install it.
Because npm is bundled with Node.js you just need to install Node.js to get up and running.
While you can browse to the Node.js website, download the installer and install it that way,
we recommend using a tool called nvm which stands for Node Version Manager.
You can find nvm at github.com/nvm-sh.
This tool will enable you to install
and use different versions of Node.js,
depending on the requirements
of the software you’re working with.
If you’re using macOS or Linux,
you can open your default terminal application
and install nvm by running the nvm install script,
which you can copy from the Installing
and Updating section of the nvm README file.
Once it is installed, you can use the nvm install command to install the Node.js and npm versions you need.
Here on a Windows machine, you will need to install the Chocolaty Package Manager for Windows to install nvm.
First, open PowerShell with administrator privileges by right-clicking on the PowerShell menu item and selecting “Run as administrator”.
Then, browse to the Chocolaty setup documentation and scroll down to the Install with PowerShell
instructions.
Copy the instructions and right-click in the PowerShell window to paste them.
Once Chocolaty is installed, use the following command to install nvm.
Choco install -y nvm
Once nvm is installed, use the nvm install command to install the required Node.js and
npm versions.
To install Node.js and npm, use the nvm install command with the version number you want to
install.
At the time of this tutorial, the current stable version of Node.js is version 18.
So you would say nvm install 18.
You can also run nvm list to see which versions of Node.js are installed.
Because nvm allows you to run multiple versions of Node.js, you need to tell nvm which version
you want to use.
You can do this by running the nvm use command followed by the version number.
You can also set the default version of Node.js and npm
by using the nvm alias command.
And then to check which versions of Node.js and npm
are enabled by running the following commands,
node -v and npm -v.
And that wraps up this tutorial on installing Node.js and npm.
Happy coding!

Introduction

Hey there, and welcome to Learn WordPress.

In this tutorial, you’re going to learn how to install Node.js and npm on your computer for local WordPress development.

You will learn why you should install this software, what the requirements are, and how to install Node.js and npm on your computer.

Why install node.js and npm?

Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside of a web browser. While this might not be specifically applicable to developing with WordPress, Node.js also comes bundled with npm. npm is both a command line tool and package manager for JavaScript, and it allows you to install and manage JavaScript packages, aka dependencies, as well as run build tools, execute code linters and automate many other processes during development. Many WordPress development projects rely on npm including Gutenberg, the create-block tool, the wp-env local WordPress environment and the new wp-now local WordPress environment.

Getting Node.js and npm installed on your computer is just like installing other development tooling like SVN or Git. You don’t have to install them to develop with WordPress, but ultimately it will make your life easier if you do.

Requirements

In order to use Node.js and npm, you’re going to need access to a terminal to run commands. Depending on your operating system, you might already have an existing terminal installed.

MacOS and or any Linux users can use the terminal that ships with the operating system.

If you are a Windows user, and you don’t already have a terminal solution, we recommend installing and using PowerShell, as it’s more beginner-friendly, and more tailored to the Windows operating system.

To install PowerShell, either open the Microsoft Store, and search for PowerShell, or browse to https://microsoft.com/powershell, browse to Setup and Installation, and then follow the instructions to download and install it.

Installing Node.js.

Because npm is bundled with Node.js, you just need to install Node.js to get up and running.

While you can browse to the Node.js website and download the installer, we recommend using a tool called nvm, which stands for Node Version Manager, which you can find at https://github.com/nvm-sh/nvm.

This will enable you to install and use different versions of Node.js, depending on the requirements of the software you’re working with.

Installing NVM

MacOS and Linux

If you are using MacOS or Linux, you can open your default terminal application, and install nvm by running the nvm install script, which you can copy from the nvm documentation.

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

Once it is installed, you can use the nvm install command to install the Node.js and npm versions you need.

Windows

If you are on a Windows machine, you will need to install the Chocolatey package manager for Windows to install nvm.

First open Powershell with administrator permissions by right-clicking on the Powershell menu item and selecting Run as administrator.

Browse to the Chocolatey setup documentation, and scroll down to the Install with PowerShell Instructions.

Copy the instructions and right click in the Powershell window to paste them.

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

Hit the Enter key to run the command.

Once Chocolatey is installed, use the following command to install nvm.

choco install -y nvm

Once nvm is installed, use the nvm install command to install the required Nodejs and npm versions.

While installing nvm via Chocolatey is recommended, as it allows you to more easily install other web development software you might need, it’s also possible to use nvm-windows, which does not require Chocolatey See the Additional Resources section for more information.

NVM usage

To install Node.js and npm, use the nvm install command with the version number you want to install. At the time of this tutorial, the current stable version of Node.js is version 18.

As newer stable versions of Node.js are released, you can safely replace the version number with the newer version number.

nvm install 18

You can also run nvm list to see which versions of Node.js are installed.

nvm list

Because nvm allows you to run multiple versions of Node.js, you need to tell nvm which version you want to use. You can do this by running the nvm use command, followed by the version number.

nvm use 18

This will set the version of node.js for the current terminal instance to version 20.

You can also set the default version of node.js and npm to use by running the nvm alias command.

nvm alias default 18

And then check which versions of node.js and npm are enabled by running the following commands

node -v
npm -v

This is useful if you are working on multiple projects that require different versions of Node.js and npm.

Conclusion

And that wraps up this tutorial on installing Node.js and npm.

Happy coding!

Additional Resources

  • nvm-windows: A node.js version management utility for Windows, that doesn’t require installing Chocolatey.

Workshop Details


Presenters

Jonathan Bossenger
@psykro

WordPress Developer Educator at Automattic, full-time sponsored member of the training team creating educational content for developers on Learn WordPress. Husband and father of two energetic boys.