How to Deploy a Node.js Application to the Cloud

Building a Node.js application locally is only the first step. To make your application accessible to users on the internet, you need to deploy it to a production server.

In this tutorial, you'll learn how to prepare a Node.js application for production, store it on GitHub, configure the required environment variables, deploy it to a cloud hosting platform, and make it accessible through a public URL.

We'll use a simple Express.js application as an example.

Affiliate Disclosure: This tutorial contains an affiliate link. If you sign up for a recommended service through the link, LearnCodePro may earn a commission at no additional cost to you.

What You'll Learn

By completing this tutorial, you will learn how to:

  • Prepare a Node.js application for production
  • Create a GitHub repository
  • Configure a Node.js application for cloud deployment
  • Use environment variables
  • Configure the application start command
  • Deploy a Node.js application to Cloudways
  • Connect a custom domain
  • Troubleshoot common deployment problems

Prerequisites

Before starting, you should have:

  • Basic knowledge of JavaScript
  • Basic knowledge of Node.js
  • Node.js and npm installed on your computer
  • A Node.js or Express.js project
  • A GitHub account
  • A Cloudways account

If you don't already have a cloud hosting provider, you can check out Cloudways:

Check Cloudways for Node.js Hosting

This is an affiliate link. LearnCodePro may earn a commission if you purchase through this link.

1. Create a Node.js Application

Let's start with a simple Express.js application.

Create a new project:

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

Install Express:

npm install express

Create a file named server.js.

Add the following code:

const express = require("express");

const app = express();

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

app.get("/", (req, res) => {
  res.send("Hello from my Node.js application!");
});

app.get("/api/status", (req, res) => {
  res.json({
    success: true,
    message: "Node.js application is running"
  });
});

app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

The important part here is:

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

During local development, the application will use port 3000 if no environment variable is provided.

In production, the hosting provider can provide the port through the PORT environment variable.

2. Configure package.json

Open your package.json file.

Make sure it contains a start script similar to this:

{
  "name": "node-deployment-demo",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^5.0.0"
  }
}

The exact Express version may be different depending on when you create the project.

The important part is:

"scripts": {
  "start": "node server.js"
}

The hosting platform can use this command to start your application.

3. Test the Application Locally

Before deploying anything, test the application on your computer.

Run:

npm start

You should see something similar to:

Server running on port 3000

Open your browser and visit:

http://localhost:3000

You should see:

Hello from my Node.js application!

You can also test the API endpoint:

http://localhost:3000/api/status

You should receive a JSON response similar to:

{
  "success": true,
  "message": "Node.js application is running"
}

If the application doesn't work locally, fix the problem before attempting deployment.

4. Create a .gitignore File

Before uploading the project to GitHub, create a file named:

.gitignore

Add:

node_modules/
.env
.env.local
npm-debug.log

The node_modules directory doesn't need to be uploaded because the hosting server can install the dependencies from package.json.

You should also avoid uploading .env files containing passwords, API keys, database credentials, or other secrets.

5. Initialize Git

Inside your project directory, run:

git init

Add the files:

git add .

Create your first commit:

git commit -m "Initial Node.js application"

6. Create a GitHub Repository

Log in to GitHub and create a new repository.

For example:

node-deployment-demo

Then connect your local project to the GitHub repository.

Your commands may look like:

git remote add origin YOUR_GITHUB_REPOSITORY_URL

Then:

git branch -M main
git push -u origin main

After the push completes, verify that your Node.js project files are visible in the GitHub repository.

7. Prepare Your Environment Variables

Production applications commonly require environment variables.

For example:

DATABASE_URL=your_database_connection
JWT_SECRET=your_secret_key
NODE_ENV=production

Never hard-code sensitive credentials directly into your source code.

For example, avoid:

const password = "my-secret-password";

Instead, use:

const password = process.env.DB_PASSWORD;

Then configure the actual value through your hosting platform's environment-variable settings.

8. Choose a Cloud Hosting Provider

A Node.js application needs a server environment capable of running Node.js.

For production applications, a managed cloud hosting platform can simplify server configuration, deployment, application management, and scaling.

Recommended Cloud Hosting

Cloudways provides managed cloud hosting options that can be used for deploying web applications.

Check Cloudways

Affiliate link: LearnCodePro may earn a commission if you sign up through this link.

Choose a hosting plan that matches the expected traffic and resource requirements of your application. You don't necessarily need a large server for a small learning project.

9. Create Your Cloudways Application

After creating or logging into your Cloudways account:

  1. Open the Cloudways dashboard.
  2. Start the process of creating a new application.
  3. Select the appropriate Node.js application option.
  4. Choose the required server and cloud infrastructure.
  5. Select a server location close to your primary users when possible.
  6. Complete the application creation process.

The exact dashboard interface and available options may change over time, so follow the current options shown in your Cloudways account.

10. Connect Your GitHub Repository

Once your application environment is ready, connect your GitHub repository.

You will generally need to provide:

  • Repository information
  • Branch name
  • Authentication information if required
  • Deployment directory or application path if requested

For this example, our production branch is:

main

Make sure the repository contains:

package.json
server.js
.gitignore

and any other files required by your application.

11. Configure the Node.js Application

Your hosting environment needs to know how to install and start your application.

The installation process should use:

npm install

The application should be started using:

npm start

Because our package.json contains:

"start": "node server.js"

the hosting environment knows that server.js is the application entry point.

12. Configure Environment Variables

Go to the environment-variable section of your hosting dashboard.

Add the required production variables.

For example:

NODE_ENV=production

If your application uses MongoDB:

MONGODB_URI=your-production-mongodb-url

If your application uses JWT:

JWT_SECRET=your-production-secret

Use your actual production values.

Never publish these credentials in GitHub.

13. Deploy the Application

After configuring the application, start the deployment.

The platform will typically:

  1. Download your source code.
  2. Install dependencies.
  3. Build the application if required.
  4. Start the Node.js process.
  5. Make the application available through the assigned URL.

Monitor the deployment logs carefully.

A successful deployment should show that the application started without errors.

14. Test the Live Application

After deployment, open the public URL provided by your hosting platform.

You should see:

Hello from my Node.js application!

Then test:

/api/status

For example:

https://your-domain.com/api/status

You should receive:

{
  "success": true,
  "message": "Node.js application is running"
}

If both endpoints work, your Node.js application is successfully running in production.

15. Connect a Custom Domain

A temporary hosting URL is useful for testing, but a production application usually benefits from having its own domain.

For example:

api.example.com

or:

app.example.com

First, configure the domain in your hosting platform.

Then update the DNS records at your domain registrar according to the values provided by your hosting provider.

DNS changes may take some time to propagate.

16. Enable HTTPS

Your production application should use HTTPS.

After configuring the domain, enable the SSL certificate through your hosting provider.

Your final URL should look like:

https://api.example.com

Avoid sending sensitive information over plain HTTP.

17. Common Node.js Deployment Problems

Application starts but immediately stops

Check your application logs.

Make sure your start command is correct:

npm start

Also make sure your application actually starts a server.

Port error

Make sure you use the port supplied by the environment:

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

Don't permanently hard-code a production port unless your hosting environment specifically requires it.

Module not found

Make sure the required package is listed in package.json.

Then run:

npm install

and redeploy.

Environment variable is undefined

Check that the variable exists in your hosting environment.

For example:

console.log(process.env.MONGODB_URI);

For production debugging, avoid logging sensitive credentials.

GitHub changes are not appearing

Make sure your changes have actually been committed and pushed:

git add .
git commit -m "Update application"
git push

Then trigger a new deployment if automatic deployment is not configured.

18. Production Deployment Checklist

Before considering the deployment complete, verify:

  • Application runs locally
  • package.json contains a start script
  • .gitignore excludes sensitive files
  • Code is available in GitHub
  • Production environment variables are configured
  • Node.js application is deployed
  • Deployment logs show no critical errors
  • Homepage/API endpoint works
  • Custom domain is configured
  • HTTPS is enabled
  • Database connection works if applicable
  • Production secrets are not stored in GitHub

Conclusion

Deploying a Node.js application requires more than simply uploading JavaScript files to a server. A production deployment needs a properly configured start command, environment variables, source-code management, server configuration, domain settings, and HTTPS.

Once these pieces are configured correctly, you can use the same basic workflow for many Node.js applications, including Express APIs, REST APIs, MERN applications, and backend services.

If you're building a production Node.js application and want managed cloud infrastructure rather than configuring every server component manually, you can check Cloudways here.

This tutorial contains an affiliate link. LearnCodePro may earn a commission if you purchase through it, at no additional cost to you.