Lesson 47 of 50 – Deployment
94% Completed
94%

Node.js Application Deployment

Learn how to prepare a Node.js application for production, configure environment variables, start a server, and deploy your application to a hosting platform.

Learning Goal: By the end of this lesson, you will understand the difference between development and production, prepare a Node.js project, and learn the basic steps for deploying it online.

1. What Is Deployment?

Deployment is the process of making an application available for users to access. During development, you usually run your application on your own computer. After deployment, users can access it through a public URL or domain.

Development Environment
http://localhost:3000

Usually used by developers to build and test an application on their own computer.

Production Environment
https://your-domain.com

A deployed application can be accessed by users over the internet.

2. What Do You Need for Deployment?

  • A working Node.js application.
  • A supported Node.js version.
  • A hosting platform or server.
  • A start command for your application.
  • Environment variables for configuration and secrets.
  • A database service, if your application needs one.
  • Logs and error handling for troubleshooting.

3. Prepare a Node.js Project

For this lesson, create a simple Express application.

Step 1: Create the Project

mkdir node-deployment-demo
cd node-deployment-demo
npm init -y

Step 2: Install Express

npm install express

Step 3: Create server.js

const express = require('express');

const app = express();

const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
    res.send('Welcome to Soopro Pathshala!');
});

app.get('/about', (req, res) => {
    res.json({
        application: 'Node.js Deployment Demo',
        message: 'Application is running successfully'
    });
});

app.get('/health', (req, res) => {
    res.status(200).json({
        status: 'ok'
    });
});

app.listen(PORT, '0.0.0.0', () => {
    console.log(`Server is running on port ${PORT}`);
});
Important: Many hosting platforms assign a port through process.env.PORT. Do not assume production applications will always use port 3000.

4. Configure package.json

The hosting platform needs to know how to start your application. Add a start script to the scripts section of package.json.

{
    "name": "node-deployment-demo",
    "version": "1.0.0",
    "description": "Node.js deployment example",
    "main": "server.js",
    "scripts": {
        "start": "node server.js"
    },
    "dependencies": {
        "express": "^5.0.0"
    }
}
This is an illustrative package.json example. Keep the dependency versions generated by your actual installation and commit the generated package-lock.json file as well.

5. Test the Application Locally

Run the application using the start command.

npm start

Open these URLs in your browser:

  • http://localhost:3000/
  • http://localhost:3000/about
  • http://localhost:3000/health

Expected Output

For the home page:

Welcome to Soopro Pathshala!

For the health endpoint:

{
    "status": "ok"
}

6. Development vs Production

Development Production
Used while building the application. Used by real users.
Often runs on localhost. Runs on a server or hosting platform.
May use test data. Must protect real user data.
Debugging output can be more detailed. Errors must not expose sensitive information.
Local configuration may be used. Production configuration and secrets are required.

7. Use Environment Variables

Production applications should receive their configuration from the hosting environment. Avoid hardcoding database passwords, API keys, and JWT secrets.

const PORT = process.env.PORT || 3000;
const NODE_ENV = process.env.NODE_ENV || 'development';

console.log(`Environment: ${NODE_ENV}`);

On your hosting platform, configure the required environment variables through its settings or secret-management system.

Never commit a real .env file or print passwords, API keys, database credentials, or JWT secrets in application logs.

8. Deploy to a Hosting Platform

Many Node.js hosting providers let you connect a Git repository, install dependencies, and run a start command. The exact interface varies by provider.

General Deployment Steps
  1. Create an account with a Node.js-compatible hosting provider.
  2. Push your project to a Git repository.
  3. Create a new web service or application on the hosting platform.
  4. Connect your repository and choose the correct project directory.
  5. Set the build or installation command, if required.
  6. Set the start command to npm start.
  7. Configure environment variables.
  8. Deploy the application.
  9. Check the deployment logs for errors.
  10. Open the public URL and test the endpoints.

Example Hosting Configuration

Runtime Node.js
Install / Build Command npm install (if required by the platform)
Start Command npm start
Port Use the platform-provided PORT variable.

These are common settings, not a guarantee that every hosting provider uses the same configuration.

9. Deploy Using a VPS

A virtual private server (VPS) gives you more control over the server. You are generally responsible for installing Node.js, configuring the application process, securing the server, and setting up HTTPS.

Typical VPS Workflow

  1. Provision a Linux server.
  2. Install a supported Node.js version.
  3. Upload or clone the application code.
  4. Install production dependencies.
  5. Configure environment variables securely.
  6. Start the application with a process manager or service manager.
  7. Configure a reverse proxy and HTTPS.
  8. Set up logs, monitoring, and backups as needed.

Install Production Dependencies

npm ci --omit=dev

Run this command in the application directory when a compatible package-lock.json file is available. It installs the locked production dependencies without development dependencies.

A VPS deployment also requires server administration and security configuration. Installing Node.js and running the application alone is not a complete production deployment.

10. What Is a Process Manager?

A process manager can keep an application running and help restart it after a crash or server reboot, depending on its configuration. PM2 is one commonly used Node.js process manager.

Install PM2

npm install -g pm2

Start the Application

pm2 start server.js --name node-demo

View Running Applications

pm2 list

View Logs

pm2 logs node-demo

Restart the Application

pm2 restart node-demo

PM2 is an optional tool. Many managed hosting platforms already handle application processes for you.

11. Deployment Security Checklist

Before Going Live
  • Use a supported Node.js version.
  • Keep dependencies updated and review security advisories.
  • Configure secrets outside source code.
  • Use HTTPS for public traffic.
  • Validate and sanitize incoming data.
  • Use secure authentication and authorization.
  • Do not expose stack traces or secrets to clients.
  • Restrict database network access and privileges.
  • Configure suitable logging and monitoring.
  • Back up important data and test recovery procedures.

12. Common Deployment Errors

Error Possible Solution
Application fails to start. Check the start command and deployment logs.
Cannot find module. Check package.json and install required dependencies.
Port binding error. Use the port assigned by the hosting platform.
Environment variable is undefined. Check its name and whether it is configured in the hosting settings.
Database connection fails. Verify credentials, network access, database availability, and configuration.
Application works locally but not online. Check runtime versions, filesystem paths, environment variables, and logs.

13. Practical Assignment

Assignment: Deploy an Express Application
  1. Create a Node.js project with Express.
  2. Create a home route and an about route.
  3. Add a health-check endpoint.
  4. Configure the start script in package.json.
  5. Make the server use process.env.PORT.
  6. Push the code to a Git repository.
  7. Deploy it to a Node.js-compatible hosting platform.
  8. Configure environment variables.
  9. Test the deployed public URL.
  10. Write down any deployment errors and how you fixed them.

14. Summary

  • Deployment makes an application available to users.
  • Test the application locally before deploying.
  • Use npm start to run the configured start script.
  • Use process.env.PORT for the hosting platform's port.
  • Configure production secrets securely.
  • Use logs to diagnose deployment problems.
  • Protect the production application with appropriate security measures.

Test Your Knowledge – Deployment

Choose the correct answer for each question.

1. What does deployment mean?

2. Which command runs the start script in package.json?

3. How should a production application receive its secret credentials?