DEV Community

ShardCloud
ShardCloud

Posted on AI-assisted

What a Node.js App Needs Before You Deploy It (with a ShardCloud Walkthrough)

Your Express app runs fine with node index.js on your laptop. Then you deploy it and you get a 502, an app that never finishes starting, or an app that dies every few hours. Most of the time the code is fine. The app just assumes things that are true on your machine and false inside a container.

This post covers those assumptions first, as a checklist you can use on any platform. Then it walks through a real deploy on ShardCloud, so you can see where each item shows up.

Part 1: The checklist

1. Read the port from the environment

Platforms decide which port your app listens on and pass it in as PORT. A hardcoded 3000 is the most common reason a deploy "works" in the logs but returns 502.

const port = Number(process.env.PORT) || 3000;
Enter fullscreen mode Exit fullscreen mode

Keep the fallback so node index.js still works locally.

2. Listen on 0.0.0.0, not localhost

Inside a container, localhost means "only reachable from inside this container". The platform's proxy sits outside it, so it can't reach an app bound to 127.0.0.1.

app.listen(port, "0.0.0.0", () => {
  console.log(`Listening on ${port}`);
});
Enter fullscreen mode Exit fullscreen mode

Express binds to all interfaces when you pass no host. Be explicit anyway: some frameworks and dev servers (Vite, Next's next dev) default to localhost.

3. Don't upload node_modules

Your node_modules was built for your OS and CPU. Packages with native code (bcrypt, sharp, sqlite3, canvas) compile a binary at install time, and a binary built on Windows or macOS won't load on the platform's Linux. It's also usually the largest folder in your project.

Ship package.json (and the lockfile, if your platform uses it) and let the platform install. Everything the app needs at runtime must be in dependencies, not only in devDependencies.

4. Keep secrets in environment variables

Database URLs, API keys and tokens go in the platform's environment variables, never in the code or the zip you upload. Read them the same way you read PORT:

const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
  throw new Error("DATABASE_URL is not set");
}
Enter fullscreen mode Exit fullscreen mode

Failing at startup with a clear message beats failing on the first request with undefined.

5. Log to stdout, and add a health route

Platforms collect what your process prints. console.log and console.error are enough; you don't need log files. A small route that returns 200 lets you (and any uptime checker) tell "the app is up" apart from "the app is up but broken":

app.get("/health", (req, res) => {
  res.status(200).json({ status: "ok" });
});
Enter fullscreen mode Exit fullscreen mode

6. Shut down cleanly

When a container is stopped or restarted, the process gets SIGTERM first and is killed a little later. If you handle it, requests in flight can finish and database connections close properly:

const server = app.listen(port, "0.0.0.0");

process.on("SIGTERM", () => {
  console.log("SIGTERM received, closing server");
  server.close(() => process.exit(0));
});
Enter fullscreen mode Exit fullscreen mode

7. Know your memory budget

A container has a hard memory limit. When the process goes over it, it is killed, not slowed down. Watch memory use while the app is under load, and size the limit from that number rather than from a guess.

Part 2: Deploying an Express API to ShardCloud

Here is a complete example with all of the above, deployed to ShardCloud. You'll need Node.js locally and a ShardCloud account.

The app

mkdir hello-api && cd hello-api
npm init -y
npm install express
Enter fullscreen mode Exit fullscreen mode

package.json:

{
  "name": "hello-api",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.21.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

index.js:

import express from "express";

const app = express();
const port = Number(process.env.PORT) || 3000;

app.get("/", (req, res) => {
  res.json({ message: "Hello from Node.js" });
});

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

const server = app.listen(port, "0.0.0.0", () => {
  console.log(`Listening on ${port}`);
});

process.on("SIGTERM", () => {
  console.log("SIGTERM received, closing server");
  server.close(() => process.exit(0));
});
Enter fullscreen mode Exit fullscreen mode

Run npm start and open http://localhost:3000 to check it works.

The config file

ShardCloud reads a plain-text file named .shardcloud in the project root. It tells the platform how to run your app:

DISPLAY_NAME=Hello API
DESCRIPTION=A small Express API
MAIN=index.js
MEMORY=512
VERSION=recommended
SUBDOMAIN=helloapi
Enter fullscreen mode Exit fullscreen mode

What each line does:

  • MAIN is the entry file. The platform detects that it's a Node.js app from the .js extension and starts it with node index.js. A .ts file is started with tsx, so TypeScript runs without a build step.
  • MEMORY is the memory limit in MB (item 7 of the checklist). Apps that serve a website need at least 512 MB; background apps such as bots can go down to 256 MB.
  • VERSION picks the Node.js version: recommended, latest, or a specific version from the list the dashboard shows.
  • SUBDOMAIN gives the app a public address, helloapi.shardweb.app here. Letters and numbers only: no hyphens.

If you need a build step (TypeScript compiled with tsc, a framework build), replace MAIN with LANGUAGE=node and a CUSTOM_COMMAND:

LANGUAGE=node
CUSTOM_COMMAND=npm run build && npm run start
Enter fullscreen mode Exit fullscreen mode

How the checklist maps to the platform

  • Port: every ShardCloud container sets PORT=80. The code above reads it, so locally it uses 3000 and in production 80, with no change.
  • Dependencies: on each start, the container installs your dependencies when package.json or the lockfile has changed, or when node_modules is missing. It picks the package manager from the lockfile: yarn.lock means Yarn, pnpm-lock.yaml means pnpm, bun.lockb means Bun, and anything else means npm.
  • Environment variables: set them in the app's settings in the dashboard, or with the CLI (shown below).
  • Logs: whatever the app prints shows up in the dashboard's log view, live.
  • Crashes: if the process exits unexpectedly, the platform restarts it, up to 3 times within a short period, then stops it so a crash loop doesn't run forever.
  • Memory: an app that goes over its MEMORY is shut down. Going over the CPU allowance only throttles it.

Deploy option A: upload a zip

  1. Zip the project folder without node_modules. (ShardCloud's Node.js guide also says to leave out package-lock.json.)
  2. Open the dashboard, go to Applications, click New app and upload the zip.
  3. Watch the logs. You should see the install, then Listening on 80.
  4. Open https://helloapi.shardweb.app. HTTPS is set up for you.

Deploy option B: the CLI

The CLI zips the current folder for you:

# macOS, Linux, WSL
curl -fsSL https://cli.shardcloud.app | bash
# Windows (needs npm)
npm install -g shard-cloud-cli

shardcloud auth login
shardcloud app create          # zips and deploys the current folder, and links it to the new app
Enter fullscreen mode Exit fullscreen mode

app create writes a .shardcloud.json file that links the folder to the app, so later commands don't need its ID:

shardcloud app env set DATABASE_URL=postgres://...   # adds to the existing variables
shardcloud app logs                                  # stream the logs
shardcloud app commit                                # deploy the folder again after a change
Enter fullscreen mode Exit fullscreen mode

Add .shardcloud.json to your .gitignore: it's a local link, not part of the app.

Deploy option C: push to GitHub

In the dashboard, connect your GitHub account (Config → Integrations), then pick the repository and branch in the app's Deploys section. Every push to that branch deploys the new files.

One thing that catches people: restarting after a deploy is off by default. The new files are in place, but the running process keeps the old code until you restart the app or turn on the optional auto-restart on deploy.

Troubleshooting

The logs say "Listening" but the URL returns 502. Check items 1 and 2: the app must listen on process.env.PORT and on 0.0.0.0. Also check that SUBDOMAIN is set in .shardcloud.

Cannot find module 'something'. The package is missing from dependencies, or it is only in devDependencies.

A native module fails to load (invalid ELF header, was compiled against a different Node.js version). You uploaded node_modules. Delete it from the zip and deploy again.

The app restarts every few hours. Look for out-of-memory in the logs. Either the app leaks memory or MEMORY is too low for its real usage.

Pushed to GitHub but nothing changed. Restart the app, or turn on auto-restart on deploy (see option C).

Wrapping up

The checklist in Part 1 is what makes a Node.js app portable: if your app reads its port and secrets from the environment, binds to 0.0.0.0, lets the platform install its dependencies, logs to stdout and handles SIGTERM, it will run with few changes on most platforms that run containers. The ShardCloud part only adds one config file on top.

If you've hit a deploy problem that isn't on this list, share it in the comments.

Top comments (1)

Collapse
 
irck_ysia_d79705b3a189af9 profile image
Irck Ysia •

Solid #node writeup on "What a Node.js App Needs Before You Deploy It (with a ShardCloud Walkt"! Native Node 22+ APIs make zero-dependency services so clean—I've been building a raw-fetch Telegram bot + static site generator all weekend myself. Dropping a heart!