The deployment of a Node.js application involves setting up the environment, installing dependencies, and configuring the app to run reliably. npm, or Node Package Manager, is used to manage dependencies, scripts, and workflows in Node.js applications, making it an essential tool for deployment. This guide will walk you through deploying a Node.js application using npm. So read through this article and know all: from preparation to deployment of your Node.js app using npm.
Prerequisites
Before you launch a Node.js application, make sure you have these things in place:
- Install Node.js and npm on your server or local environment.
- Access to a server or reliable Node.js hosting environment.
- A Git repository or zip file of your Node.js application code.
How to Deploy Node.js Application Using npm
1. Prepare Your Server
Install Node.js and npm: If the server doesn’t have pre-installed Node.js and npm, then install npm and Node.js using the commands below:
sudo apt update
sudo apt install nodejs npm
Check Installation
Verify whether you have successfully installed Node and npm through these commands:
node -v
npm -v
If the installation is successful, then it will print the installed version of both Node.js and npm.
2. Clone or Upload Your Node.js Project
If using Git, clone your project repository to the server:
git clone <repository-url>
cd <project-folder>
If you are not using Git, you have to manually upload your project files by using SFTP or some other method and then find yourself in the project folder.
3. Install Project Dependencies Using npm
Now that you have created a package.json file in your project directory, run the following command to install all the dependencies specified in that file:
npm install
This npm command reads the package.json and installs all the necessary modules. This creates a node_modules folder containing your dependencies.
4. Set Up the Application for Deployment
For deploying the Node.js application using npm, you need to set environment variables. Applications that require configuration, for example, database URLs, API keys, or environment-specific settings, demand setting environment variables.
It is usual to use the .env file to place environment variables in applications. Install the dotenv package if it has not been installed yet:
npm install dotenv
Load variables in your Node.js application using the given command:
require('dotenv').config();
Example of a.env File
PORT=3000
DATABASE_URL=mongodb://localhost:27017/yourdb
To test that environment variables are successfully loaded, create a start script in package.json using the following command:
"scripts":
"start": "node -r dotenv/config server.js"
}
5. Run the Application
Use the npm start command to launch the application. This command will use the start script defined in your package.json file:
npm start
If your app runs without errors, you should see a message indicating that the server is running (e.g., “Server is listening on port 3000”).
6. Configure the Application for Production
Once your application runs successfully, the very next step is configuring it. If you want your Node.js application to run with optimal performance, optimize it by setting the NODE_ENV environment variable to production. This will prevent development-specific configurations from existing and allow optimizations with production.
7. Run the Application in the Background
Running the app in the background makes sure that it would be running on the computer even after one closes the active session in the terminal. There are several prominent tools that cater to the above purpose; among such popular tools are PM2 and Screen.
8. Set Up Automatic Restarts on Reboots
If your server reboots, you can use Systemd or PM2 and configure accordingly to start your application automatically. Here’s the command for PM2:
pm2 startup
pm2 save
9. Verify the Application
Open your server’s IP address and port in a web browser (e.g., http://<server-ip>:3000), or check the server logs with PM2:
pm2 logs
Now that you have read this article, you’ll be familiar with:
1. Install Node.js and npm on your server.
2. Clone or upload your project to the server.
3. Install dependencies with npm install.
4. Configure environment variables and set NODE_ENV to production.
5. Run the application in the background
6. Set up automatic restarts with PM2’s feature.
By following these steps, you can manage and deploy your Node.js application using npm in a production environment, ensuring it runs smoothly and remains accessible to users.