Installing and configuring Node.js
The first step of preparing our project is to install Node.js. There are two ways to do this:
- One option is to install Node Version Manager (NVM). The benefit of using NVM is that you can easily run multiple versions of Node.js side by side, which handles the installation process for you on nearly all UNIX-based systems, such as Linux and macOS. Within this book, we do not need the option to switch between different versions of Node.js.
- The other option is to install Node.js via the package manager of your distribution if you are using Linux. The official PKG file is for Mac, while the MSI file is for Windows. We are going to use the regular Linux package manager for this book as it is the easiest method.
Note
You can find the Downloads section of Node.js at the following link: https://nodejs.org/en/download/.
We are going to be using the second option here. It covers the regular server configurations and is easy to understand. I will keep this as short as possible and skip all the other options, such as Chocolatey for Windows and Brew for Mac, which are very specialized for those specific operating systems.
I assume that you are using a Debian-based system for ease of use with this book. It has got the normal APT package manager and repositories for easily installing Node.js and MySQL. If you are not using a Debian-based system, you can look up the matching commands to install Node.js at https://nodejs.org/en/download/package-manager/.
Our project is going to be new so that we can use Node.js 14, which is the current LTS version:
- First, let's add the correct repository for our package manager by running the following command:
curl -fsSL https://deb.nodesource.com/setup_14.x | sudo bash -
- Next, we must install Node.js and the build tools for native modules using the following command:
apt-get install -y nodejs build-essential
- Finally, let's open a Terminal and verify that the installation was successful:
node --version
Note
Installing Node.js via the package manager will automatically install npm.
Great! You are now set to run server-side JavaScript with Node.js and install Node.js modules for your projects with npm.
All the dependencies that our project relies on are available at https://npmjs.com and can be installed with npm or Yarn. We will rely on npm as it is more widely used than Yarn. So, let's continue and start using npm to set up our project and its dependencies.