Deploying a Node.js Application
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 cloud server with a Linux-based OS.
Installing the software Copy link
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 updateThen install Node.js:
sudo apt-get install nodejsAnd install npm to manage Node.js packages:
sudo apt-get install npmTo make sure the software installation was successful, check the versions of Node.js and npm:
nodejs -v
npm -vTransferring the code to the server Copy link
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 --versionIf Git is not installed, install it:
sudo apt install gitSuppose, the project code is stored on GitHub. Clone it to the server this way:
git clone https://github.com/contentful/the-hostman-app.nodejsAfter the git clone command, specify the repository where the project is stored.
Installing the dependencies Copy link
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 installThis 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 Copy link
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 -gThen start the Node.js server using pm2:
sudo pm2 start app.js --name "web-app" --watchIn 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 saveYou 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.
Conclusion Copy link
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.
By the way, with Hostman, you can run your workloads on efficient VPS USA or Netherlands VPS that support low latency for EU-based users. For learning, test deployments, and pet projects, you can use budget VPS hosting that scales smoothly as your needs grow.