How to Install Node.js and Launch Your First Node App

How can we help?
< All Topics
Print

How to Install Node.js and Launch Your First Node App

The popularity of Node.js has increased recently, in which makes ‘how to install node.js’ as a hot topics around the internet. But, let’s discuss about what Node.js is.

Node.js is a server-side Javascript that makes dev can write the front-end and back-end of web applications in Javascript more easily and efficiently. Node.js is an open-source application under MIT License.

In this tutorial, we will learn how to install node.js using the ubuntu repository, PPA, or NVM.
And we will learn how to launch your simple app using node.js.

  1. Using Ubuntu repository. We will install using stable nodejs with command apt
  2. Install nodejs using PPA (Personal Package Archive) repository.
  3. Use and install multiple nodejs using NVM (NodeJS Version Manager).

Install Node.js using Ubuntu Repository

Please ssh your server on Cloudraya and make sure you have created a security profile to allow accessing your server.

After you login into your server, run the command below:

# apt update
# apt install nodejs

Run the command below to find nodejs version:

# nodejs -v

If you want to find some module or package of nodejs, we can use NPM (NodeJS Package Manager). You can install it using command :

#apt install npm
# npm -v

Now your server is ready to use nodejs.

Install Node.js Using PPA (Personal Package Archive) Repository

If we want to use nodejs with latest version, we can use nodejs using PPA Repository that maintained by NodeSource.

We can find version nodejs on https://github.com/nodesource/distributions.
Run the command below to install nodejs v.12.

# curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs

If we use nodejs from PPA, npm automatically installed on your server.

Install Node.js Using NVM (NodeJS Version Manager)

Nodejs allow us to install multiple nodejs version on the server.
We recommend you to use nodejs with NodeJs Version Manager (NVM). After we install nodejs using NVM, we can change the default nodejs version if we install multiple nodejs on the server.

Below is how to install nodejs using NVM:

# apt install build-essential libssl-dev
# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

After the installation is completed, please allow NVM to run on bash profile user .

# source ~/.profile

List the nodejs version using the command below:

# nvm ls-remote

On this article we will use nodejs LTS version with version 14.

# nvm install 14.15.3

As we know, using NVM we can run multiple nodejs version on the server.
Now we will install nodejs version 8.

# nvm install 8.17.0

Please run the command below to find nodejs version that installed on the server.

We can see if the default nodejs on the server is nodejs version 14.
If we want to change the nodejs version 8, we can use the command below:

# nvm use 8

Launch Your First Node App

After we know how to install nodejs on the server using many different way. Now we can learn how to launch your first node App on the server.

Create folder nodejsapp andn create a file as app.js

#mkdir nodejsapp
#cd nodejsapp
#nano app.js

Append the code below on file app.js

console.log('This NodeJS Application in Cloudraya');

Press Ctrl + x to save and quit from the editor.

Run the command below to execute the code of app.js

#chmod +x app.js
#nodejs app.js or node app.js (if you use NVM)

Create Local Webserver

Nodejs is client side server language which means we can run it without any webserver like nginx or apache. With nodejs we can create a local webserver on the server.

First, we create a file server.js and append the code below on the file

# nano server.js
var http = require('http');
var server = http.createServer(function (req, res) {
        res.writeHead(200,{'Content-Type': 'text/plain'});
        res.end('NodeJS App');
});
server.listen(5050);
console.log('Server is running at http://localhost:5050/');

Exit from the edit by press Ctrl + x to save and quit.

The code that we have appended on the file means to make a server connection on port 5050. Please run the command below to run the local webserver

# nodejs server.js / node server.js (NVM)

Now we can reach the node app via http://ip_address_server:5050 from any browser.

If you see the message on above, it means you successfully to launch node app on Cloudraya server.
Please feel free to submit your question to our Support if you need more assistance.

Table of Contents

Comments are closed.

Ready, Set, Cloud

Ready, Set, Cloud