How do you build and run a Node.js application?

BeginnerHands-onNode.js builds (npm, package.json)
Answer

Short answer

Install dependencies with npm ci (or npm install), run tests with npm test, build if needed with npm run build, and start the app with npm start or node server.js.

Step by step

node -v && npm -v          # check versions (e.g. Node 20 or 22)

npm ci                     # install exact versions from package-lock.json
npm test                   # run the tests
npm run build              # only if the project has a build step (React, TypeScript)
npm start                  # runs the "start" script, e.g. node server.js
# Server listening on port 3000

npm install vs npm ci

npm install npm ci
Used by Developers on their laptop CI pipelines and Docker builds
Reads package.json (can update the lock file) package-lock.json only — fails if the two don't match
Speed Slower Faster; deletes node_modules and installs clean
Result May install slightly different versions Exactly the same versions every time

Frontend vs backend apps

  • Backend (Express, NestJS): you run a server process — node server.js.
  • Frontend (React, Angular, Vue): npm run build creates static files (HTML, CSS, JS) in build/ or dist/. You don't run Node in production — you serve those files with Nginx or a CDN.

Example — Dockerfile for a Node.js API

FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev          # only production dependencies, cached as a layer
COPY . .
ENV NODE_ENV=production
EXPOSE 3000
CMD ["node", "server.js"]

What to say in the interview

"In the pipeline we run npm ci so we get the exact versions from the lock file, then npm test, and npm run build if it's a TypeScript or frontend project. A backend runs as node server.js or npm start. A React frontend is built into static files and served by Nginx. In Docker, we copy package.json first and run npm ci before copying the code, so the dependency layer is cached."

Follow-up questions you may get

  • Why CMD ["node", "server.js"] instead of npm start in Docker? Node then runs as PID 1 and receives stop signals directly, so the container shuts down cleanly.
  • How does a Node app read config? From environment variables via process.env.DB_HOST.
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Node.js apps: npm & package.json

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