# How to Deploy a Node.js Application with xCloud

> Deploy a Node.js application to xCloud from a Git repository: branch, runtime, serving mode, port, environment variables and build script, plus where the app logs are and how to fix a 502.

You can deploy a [**Node.js**](/node-js-hosting/) application to [**xCloud**](/) directly from a public or private Git repository. xCloud analyzes the repository, selects Node.js as the application type when it detects a supported Node.js project, and prepares the deployment settings for you to review.

This guide shows how to deploy an Express-style Node.js application with **Deploy via Git**, including the branch, runtime, serving mode, port, environment variables, build script, and post-deployment settings.

## Prerequisites

Before you begin, make sure you have:

-   An active xCloud account.
-   A connected server with enough resources for your application.
-   A Git repository containing your Node.js application.
-   Access to the repository if it is private.
-   A start script in `package.json`, such as `npm run start`, for an SSR or API application.

## Step 1: Start a Deploy via Git deployment

Open the target server in xCloud, select **Add New Site**, and choose **Deploy via Git**. The deployment screen lets you search for a public repository or switch to a private repository provider.

![Add New Site with the Deploy via Git option selected in xCloud](/_landing/docs/how-to-deploy-a-node-js-application-01-deploy-via-git.png)

## Step 2: Choose the repository and confirm the app type

Paste the repository URL or select a repository from your connected Git provider. xCloud analyzes the repository and preselects an application type.

Confirm that **Node.js** is selected. If the detected type is incorrect, choose Node.js manually before continuing.

![xCloud detecting the repository and preselecting Node.js as the application type](/_landing/docs/how-to-deploy-a-node-js-application-02-repository-and-app-type.png)

> Repository detection is a starting point, not a replacement for review. Check the branch, runtime, commands, port, and environment values before deployment.

## Step 3: Choose a branch and domain

Select the branch that xCloud should deploy. For the first deployment, you can use the generated **Staging domain (no DNS)**. To deploy on your own hostname instead, select **Use a live domain instead** and complete the domain configuration shown in xCloud.

![Selecting the deploy branch and a staging domain in xCloud](/_landing/docs/how-to-deploy-a-node-js-application-03-domain-and-branch.png)

The staging domain is useful when you want to verify the application before changing DNS for a production domain.

## Step 4: Configure how the application is served

In **Build & serving**, select the mode that matches the application:

| Serving mode | Use it for |
| --- | --- |
| **SSR (Node process)** | Express, NestJS, Next.js SSR, APIs, and other applications that keep a Node.js process running |
| **Static** | Applications that produce static files and do not need a persistent Node.js process |
| **Hybrid** | Applications that need both generated static assets and a running Node.js process |

For an SSR or API application, enter the command used to start the process and the port on which the application listens. The example below uses `npm run start` and port `3000`.

![The Build & serving section with SSR mode, start command and port configured](/_landing/docs/how-to-deploy-a-node-js-application-04-build-and-serving.png)

Your application must listen on the same port configured in xCloud. When the application supports it, read the port from the `PORT` environment variable instead of hard-coding it.

## Step 5: Review the Node.js runtime

Expand **Advance** and confirm the **Node.js Version**. Choose a version supported by your application and its dependencies.

The **Site User** is the Linux user that owns the site files. Keep the generated value unless you have a specific naming requirement.

![The Advance section showing the Node.js Version and Site User fields](/_landing/docs/how-to-deploy-a-node-js-application-05-node-version.png)

## Step 6: Add environment variables

Use the **Environment file (.env)** field for non-secret and secret runtime configuration required by your application. You can also specify the repository path to the environment file when it is not located at the project root.

![The Environment file (.env) field during Node.js deployment setup](/_landing/docs/how-to-deploy-a-node-js-application-06-environment.png)

For example:

```
NODE_ENV=production
PORT=3000
```

Do not commit production credentials to Git. Enter them in xCloud's environment editor and rotate any credential that was previously exposed in repository history.

Need a PostgreSQL database? On an Nginx server with managed PostgreSQL **Ready**, set **Database engine** to **PostgreSQL** when you create the site. xCloud creates the database and role and adds `DATABASE_URL` and the `DB_*` connection settings to the site's environment for you. See [How to Install Managed PostgreSQL and pgAdmin in xCloud](/docs/install-managed-postgresql-and-pgadmin-in-xcloud/).

## Step 7: Review the build and deploy script

The **Build & deploy script** runs after xCloud pulls the selected branch. Review the generated commands and adjust them to match the package manager and build process used by your repository.

![The Build & deploy script field with the generated install and build commands](/_landing/docs/how-to-deploy-a-node-js-application-07-deploy-script.png)

A common npm-based script installs dependencies and builds the project:

```
$XCLOUD_NPM install
$XCLOUD_NPM run build
```

Only keep the build command when the repository defines a build script. Applications that run directly from source may not need one.

## Step 8: Review the summary and deploy

Check the summary before starting the deployment. Confirm the repository, application type, domain, and serving mode. Then select **Deploy**.

![The deployment summary screen before selecting Deploy](/_landing/docs/how-to-deploy-a-node-js-application-08-summary-and-deploy.png)

Wait for the deployment process to finish before opening the site. If the deployment fails, inspect the deployment log for the command that failed and compare it with a successful local production build.

## Manage environment variables after deployment

Open the site and select **Environment** to review or update its runtime variables. Updating the environment of a Node.js application restarts the managed process so the new values can take effect.

![The Environment editor on the site dashboard after deployment](/_landing/docs/how-to-deploy-a-node-js-application-09-environment-editor.png)

Use one `KEY=value` pair per line. Avoid adding comments or formatting that your application's environment loader does not support.

Each Git deployment resets the code folder, but xCloud keeps an untracked `.env` in the site's root folder. Keep `.env` in your `.gitignore` anyway: a committed `.env` is replaced on every deploy, and an untracked `.env` in a subfolder is removed. See [Environment Variables (.env) and Git Deployments](/docs/environment-variables-and-git-deployments-in-xcloud/) for how a Git deployment treats your `.env` file and how to keep it.

## Update SSR configuration

For an application that runs as a Node.js process, open **SSR Configuration** to update the application port or start command. The port must be available on the server and must match the port used by the application.

![The SSR Configuration panel for updating the application port and start command](/_landing/docs/how-to-deploy-a-node-js-application-10-ssr-configuration.png)

Updating SSR configuration restarts the managed process and may cause brief downtime.

## Verify the deployment

After the deployment completes:

1.  Open the configured staging or live domain.
2.  Confirm the expected page or API response loads.
3.  Check the deployment log for build errors or missing packages.
4.  Confirm the application is listening on the configured port.
5.  Exercise one application-specific flow, such as loading a server-rendered route or calling a health endpoint.

## Find the application logs

Your Node.js application's own output (startup messages, crashes and stack traces) is in the site's **PM2 Log**. xCloud runs the application with PM2, so this is the first place to look when the site does not load.

1.  Open the site and go to **Monitoring** → **Logs** in the site menu.
2.  In the log picker, select **PM2 Log**. For a Node.js site it is usually selected already.
3.  Read the newest lines for the error the application printed when it started or stopped.

![Select 'PM2 Log' on the site's Logs page under Monitoring → Logs](/_landing/docs/how-to-deploy-a-node-js-application-image-11.png)

On an Nginx server, the same picker also has **Nginx Access Log** and **Nginx Error Log**. The Nginx error log shows whether the web server could reach your application at all. For more on the Logs page, see [View Logs and Events of Your Server and Site](/docs/view-logs-and-events-of-your-server-and-site/).

## Fix a 502 Bad Gateway after deployment

A 502 means the deployment finished but the web server could not reach your application. xCloud sends visitors to your application at `127.0.0.1` on the **Application Port**. When nothing answers there, visitors get a 502.

A common cause is an application that listens only on `localhost`. On current Node.js versions, `localhost` can resolve to the IPv6 address `::1`, so the application runs and works when you test it over SSH, but nothing answers at `127.0.0.1`.

### Step 1: Read the PM2 Log

Open **Monitoring** → **Logs** and select **PM2 Log**, as shown above. If the log shows a crash on startup, such as a missing environment variable or a module that was not installed, fix that error first. Changing the address your app listens on will not help.

### Step 2: Listen on 127.0.0.1 and the configured port

Change the server code so it listens on `127.0.0.1` and reads the port from `PORT`:

```js
const port = process.env.PORT || 3000;
app.listen(port, '127.0.0.1', () => {
  console.log(`Listening on 127.0.0.1:${port}`);
});
```

If your framework takes the address from an environment variable such as `HOST`, set it to `127.0.0.1` in the site's **Environment** page instead. Use `0.0.0.0` only if something else on the server must reach the app; it also makes the app reachable by server IP and port, not only through your domain. Confirm that the port matches **Application Port** in **SSR Configuration**.

### Step 3: Redeploy and check the site again

Push the change to the deployed branch. If the push does not start a deployment on its own, select **Deploy Now** at the top of the site page so the application restarts with the new code. **Deploy Now** is available on paid plans; on the Free plan, turn on **Enable push to deploy** in the site's Git settings and push a new commit to the deployed branch to start a deployment. Then open the domain again. The page or API response should load, and the PM2 Log should show the application listening on the configured port.

If the site still returns 502, read the PM2 Log again: a process that keeps restarting prints the reason there. For deployments that fail or do not start, see [Troubleshooting Git Deployments](/docs/troubleshooting-git-deployments-stuck-failed-or-not-triggering/).

## Troubleshooting

| Symptom | What to check |
| --- | --- |
| Build command fails | Confirm the package manager, lockfile, Node.js version, and the scripts defined in `package.json`. |
| Application deploys but returns an error | Check the process start command, the **PM2 Log** under **Monitoring** → **Logs**, required environment variables, and external service credentials. |
| Site returns 502 Bad Gateway | Make sure the application listens on `127.0.0.1` (not only `localhost`) on the configured port, then redeploy. See [Fix a 502 Bad Gateway after deployment](#fix-a-502-bad-gateway-after-deployment). |
| Static assets return 404 | Confirm the build output directory and whether the application should use Static or Hybrid serving mode. |
| New environment values are not applied | Save the Environment page and allow the managed process to restart. |

## Frequently Asked Questions

### Which Node.js version should I choose?

Choose a version supported by the application and its dependencies. If the repository declares an engine in `package.json`, use a compatible version in xCloud.

### Do I need a build command?

Only when the application compiles or generates production files. TypeScript, frontend bundles, and many frameworks require a build step; a small JavaScript API may not.

### What port should I use?

Use the port on which the application listens, such as `3000`. Keep the value consistent between xCloud's **Application Port**, the `PORT` environment variable, and the application's server code.

### Can I deploy a private repository?

Yes. Select **Private Repository**, connect the supported Git provider, and choose a repository that the connected account can access.

### Why does my Node.js app return 502 when it works over SSH?

The application most likely listens only on `localhost`, which can resolve to the IPv6 address `::1`. xCloud reaches the application at `127.0.0.1` on the Application Port. Make the application listen on `127.0.0.1` on that port, then redeploy.

### Where are my Node.js application logs?

Open the site, go to **Monitoring** → **Logs**, and select **PM2 Log**. It shows what your application prints, including startup errors and crashes.

## Next steps

Once the staging deployment works, connect the production domain, configure DNS and SSL, add production environment values, and verify the application again on its final URL.

If a deployment gets stuck, the build or start fails, or auto-deploy stops triggering, see [Troubleshooting Git Deployments](/docs/troubleshooting-git-deployments-stuck-failed-or-not-triggering/). The `.env` file path pointing at a folder instead of a file is a common cause of a failed start.

Still stuck? Contact our [**support team**](/support) for any of your queries.
