Log In

Deploying a Node.js Application

Deploying a Node.js Application
Reading time: 4 min
Hostman Team
Technical writer

Node.js is a JavaScript runtime environment for running server-side code. You can use it to write any server-side script for web applications. This tutorial will show you how to deploy a Node.js application on a Hostman server with a Linux-based OS.

Installing the software

First, you must prepare the host for the web application deployment. In this guide, we will use an Ubuntu machine as an example. 

First, get the latest updates:

sudo apt-get update

Then install Node.js:

sudo apt-get install nodejs

And install npm to manage Node.js packages:

sudo apt-get install npm

To make sure the software installation was successful, check the versions of Node.js and npm:

nodejs -v
npm -v

Transferring the code to the server

Deploying a Node.js application starts with moving the project code to the host. The most convenient way to do this is to use Git. 

Make sure you have Git installed on Ubuntu:

git --version

If Git is not installed, install it:

sudo apt install git

Suppose, the project code is stored on GitHub. Clone it to the server this way:

git clone https://github.com/contentful/the-hostman-app.nodejs

After the git clone command, specify the repository where the project is stored.

Installing the dependencies

For a Node.js application to work properly, the deployment must also include the installation of dependencies—all the packages used in the project.

Go to the root directory of your project and run:

npm install

This command will check the package.json file in the project and install the packages needed for production. After adding them to the server, you can run the application using the command:

node app.js.

Instead of app.js, specify the name of the main file of your Node.js application.

Managing application launch and updates

Your Node.js application is already running on the server. But as soon as you close the terminal, it stops. Besides, your website is not automatically updated when you change the application code. 

You can solve this problem by using the pm2 process manager. This package will make the application run in the background. In addition, the manager will monitor the code and automatically restart the server whenever you apply changes using the --watch directive. 

Install pm2 with the command:

sudo npm install pm2 -g

Then start the Node.js server using pm2:

sudo pm2 start app.js --name "web-app" --watch

In the example above, we used the name "web-app". This will be the name of the running process. You can use any other name you want. The --watch directive indicates that pm2 needs to restart the application when the source file changes. Now all updates you make in the project code will be immediately visible in production. 

There is one more thing left to do: automatically launching the web application on server startup/reboot. To do this, run the following commands:

sudo pm2 startup
sudo pm2 save

You only need to run the startup command once to generate the desired startup parameters. The save command tells pm2 to save the currently running processes. 

Now the server will continue to run regardless of whether you close the terminal. After restarting the system, all necessary processes will also be started automatically.

You can learn more about how pm2 works from the official documentation.


In this article, we have completed a Node.js deployment to an Ubuntu server. The project runs smoothly and automatically updates whenever changes are made. However, this was a very simple example of using Node.js. It is suitable for deploying home and training projects. 

You may also need additional customization when deploying, for example, installing SSL certificates or changing the Nginx configuration. The final list of actions required to deploy an application in production depends on what stack it uses and its dependencies and connections: databases, web servers, and automation utilities.