Why should a container run `node server.js` instead of `npm start`?

IntermediateConceptNode.js builds (npm, package.json)
Answer

Short answer

With npm start, npm becomes the main process (PID 1) and Node runs as its child. npm doesn't reliably forward signals like SIGTERM, so when Kubernetes stops the pod, the app doesn't get a chance to shut down gracefully — it's killed after the grace period. Running node directly means Node receives the signal itself.

What graceful shutdown looks like

const server = app.listen(PORT, "0.0.0.0");
process.on("SIGTERM", () => {
  console.log("SIGTERM received, closing server");
  server.close(() => { db.end(); process.exit(0); });   // finish in-flight requests
});

Dockerfile

CMD ["node", "dist/server.js"]          # ✅ exec form, node is PID 1
# CMD npm start                          # ❌ shell form + npm in between

For extra safety, use docker run --init or tini as PID 1 so zombie processes are reaped and signals are forwarded.

Symptom when it's wrong

Every deploy takes 30 seconds per pod (the default terminationGracePeriodSeconds) and some requests fail with 502 during rollouts.

What to say in the interview

"PID 1 must handle SIGTERM. npm doesn't forward signals reliably, so we run node directly in exec form, handle SIGTERM to drain connections, and optionally use tini. That avoids dropped requests during rolling updates."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Node.js apps: npm & package.json

More on Node.js builds (npm, package.json)