
Node.js is an open-source JavaScript runtime built on Chrome's V8 JavaScript engine that enables developers to build scalable network applications. NPM (Node Package Manager) is bundled with Node.js and manages project dependencies on a server.
This article explains how to install Node.js and NPM on a FreeBSD 14.0 server. It covers installing Node.js from the ports collection and from the pkg package manager, installing NPM, and managing multiple versions with the Node Version Manager (NVM).
Before you begin, you need to:
Choosing the right Node.js version for your project is necessary. Node.js comes in various versions, including the latest, LTS (Long-Term Support), and previous versions that cater to different needs. Below are the supported distributions to consider when choosing a Node.js version.
Node.js is available on FreeBSD 14.0 with two installation procedures using the default pkg package manage or ports collections. FreeBSD ports include the latest Node.js versions while the pkg package manager contains specific stable versions. Follow the sections below to install Node.js on your server depending on your target version.
FreeBSD ports collections allow you to build and install applications from source code. A collection offers a standardized framework and a variety of ports that contain all necessary files and instructions to download, compile, and install specific applications on your server. In the following steps, update your FreeBSD ports collection and install Node.js on your server.
Update the pkg repository catalog.
Install the portsnap package to fetch and update the default FreeBSD ports collection.
Create the /var/db/portsnap directory to store snapshots of the ports tree.
Update the ports tree to get all available Node.js versions.
List all Node.js port versions available in the /usr/ports/www directory.
Output:
Each nodeXX directory is a Node.js release line, with a matching npm-nodeXX port that provides NPM for that line.
Switch to your desired Node.js version directory. For example, version 18.
Build and install Node.js from the ports collection.
During the installation, the BATCH=yes option accepts all installation prompts while MAKE_JOBS_UNSAFE=yes disables parallel building and forces the port to compile with a single job. FreeBSD builds ports in parallel by default, so set this option only when a port fails with intermittent errors under a parallel build. When successful, your output should look like the one below.
View the installed Node.js version.
Output:
Compiling the Node.js version in your ports collection takes between 2 to 3 hours to complete depending on your system's resources.
pkg Package ManagerNode.js is available in the default pkg repositories on your FreeBSD server. However, the available versions may not be the latest. Follow the steps below to verify all available versions and install Node.js on your server.
Update the pkg repository catalog.
Search all the available Node.js versions.
Your output should look like the one below.
Install your desired Node.js version. For example, Node.js version 21.
View the installed Node.js version on your server.
Output:
NPM is the default Node.js package manager that allows you to manage and install backend packages on your server. It's bundled and installed with the Node.js package when using the pkg package manager. When using FreeBSD ports, install NPM as described in the following steps.
Verify if NPM is available on your server.
Output:
Navigate to the NPM port directory to install a specific version on your server. For example, switch to the NPM directory for Node.js version 18
Build and install NPM on your server.
When using the pkg package manager, run the following command to view all available versions.
Output:
Install a specific NPM version based on the available packages. For example, run the following command to install the latest available version.
Running a short script confirms that the runtime executes JavaScript and that the node command resolves in your shell.
Create a new test application file app.js to output a Hello World! Greetings from Vultr prompt when loaded.
Run the app.js file using Node.js.
Output:
NVM (Node Version Manager) is a Node.js management utility that simplifies the process of switching between Node.js versions for various projects. Follow the steps below to install NVM, install, and switch between multiple Node.js versions on your server.
Install all required dependencies on your server.
Download the latest installation script from the official NVM repository on your server.
Run the installation script to install NVM on your server.
Run the following commands to add NVM to your shell profile and automatically load it in every terminal session. Replace user with your actual system user.
Reload your shell profile to apply changes.
View the installed NVM version on your server.
Output:
Create a new MAKE_JOBS environment variable depending on the number of CPU cores available on your system to speed up the compilation process while running multiple jobs in parallel.
Install a specific Node.js version on your server using NVM. For example, Node.js version 21.0.0.
View the installed Node.js version.
Output:
View the installed NPM version.
Output:
Install another Node.js version such as 17.0.0.
Install the latest LTS Node.js version.
Install the latest stable Node.js version.
List all available Node.js versions on your server.
Output:
Run the following command to enable a specific Node.js version on your server. For example, Node.js version 21.0.0.
Set the default Node.js version to use on your server.
Run the following command to remove a specific version. For example, Node.js version 17.0.0.
You have installed Node.js and NPM on a FreeBSD 14.0 server and managed multiple versions using NVM to ensure flexibility between projects. You can use Node.js to build flexible and powerful applications on your server depending on your development needs. For more configurations and additional modules such as Express, visit the Node.js documentation.
0 Comments
Be the first to comment and share your perspective with the community.