Hostwinds Tutorials
Search results for:
Table of Contents
Tags: Linux
Along with the many technologies out there, such as Ruby, Python, and PHP, you may have heard of one called Node.js. Node.js is a lightweight and seemingly efficient JavaScript runtime that can be used to run JavaScript code outside of the browser. Among the other amazing features that Node.js has jam-packed into itself, it is a popular choice for websites to use. Modern-day, it is straightforward and easy to set up though it can still be intimidating. Here we will show you how to install Node.js and Node Package Manager (NPM) on your server.
This installation guide is going to rely on the curl command. If you do not have curl installed, you can type this into your console:
sudo yum install curl
Once that is installed, please proceed with the steps below.
Connect to your VPS via SSH.
Here we will want to add the Node.js repositories so that yum knows where to look for the latest Node.js when it is installing. To do this, you are going to want to type in the command:
curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
Once that is finished, you will now be able to install Node.js. To do this, you are going to want to use this command:
sudo yum install -y nodejs
This will install Node.js onto your CentOS system. You will see periodic progress while this installs. It can usually take a few minutes to complete the installation. But it will let you know once it has been completed.
After the installation of Node.js has been completed, it should have installed the Node Package Manager. To verify that both of these have been installed correctly, you can type these commands:
For Checking Nodejs Installation:
node -v
**
For Checking Node Package Manager Installation:**
npm -v
If installation had completed successfully, you should see a version number listed. If you don't see that, or you see an error message saying the command does not exist, it is likely the installation was unsuccessful.
Before installing Node.js on Debian, you will want to make sure that you have the curl command installed before proceeding. For Debian, you can install this by typing the command:
sudo apt-get install curl
After you have that installed, proceed with the steps below in getting Node.js and NPM installed on your server.
Start by connecting to your Debian installation via SSH.
Once connected, you'll want to add the repositories so that Debian knows what you are referencing when you ask it to install Node.js. To do this, you can use the command:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
It might take a while for Debian to add that repository, however once done, you can proceed to install Node.js. For that, use the command:
sudo apt-get install -y nodejs
This can also take a little bit of time to install and set up on the system. Though once done, it will let you know. To verify that the installation has been completed successfully. Exactly as it was done in the steps for CentOS, you will want to check the version numbers. Type these commands to do that,
For Checking Nodejs Installation:
node -v
**
For Checking Node Package Manager Installation:**
npm -v
If the installation was successfull, you should see a version number outputted when you type the command. If you don't see that, or you see an error message saying the command does not exist, it is likely the installation was unsuccessful.
Aside from the version testing in the guides above, you can also create a simple little Node.js document and run it to verify that everything is working correctly. To do this, there are a few steps involved which can be followed below.
While logged into your server via SSH, create a file called test.js. This can be done simply with this command:
touch test.js
After you have created that file, you will want to edit this file. In either operating system, there are various ways to do this. Here we will use the text editor nano in this guide which is a simple command-line text editor. To use nano to edit your newly created file, type the command:
nano touch.js
That command will bring the console text editor up with that file open. Here is an example that we will use to test the Node.js installation:
const t_http = require('http');
const t_port = 80;
const t_ip = '0.0.0.0';
t_http.createServer(function(a_req, a_res) {
a_res.writeHead(200, {'Content-Type': 'text/plain'});
a_res.end('Test Site');
}).listen(t_port, t_ip);
console.log('Web Server is running on ' + t_ip + ':' + t_port);
After you've typed all that out into the file, you can press Control + X at the same time to close the text editor, then press Y to save the file.
Once you have saved the file, you can now execute the file by typing the following command:
node test.js
This should display something similar to "_Web Server is running on 0.0.0.0:80_".
If you navigate your server's IP address in your web browser, you should see the test site. If you are using a browser on the same server, you can type in 127.0.0.1. Once you have verified that it looks good and shows up, you can close the Web server on your server by pressing Control + C at the same time to force close the program.
If you should have any questions or would like assistance, please contact us through Live Chat or by submitting a ticket with our Technical Support team.
Written by Hostwinds Team / April 24, 2018