Deploying a Node.js Application
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
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
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:
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
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:
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:
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
pm2 with the command:
sudo npm install pm2 -g
Then start the Node.js server using
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.