NODEJS Contents

npm Scripts: Dev/Build/Test Workflow

Learn how to design npm scripts for a clean dev/build/test workflow, avoid cross-platform pitfalls, and keep production execution predictable.

On this page

Why npm Scripts Matter

npm scripts are the simplest way to standardize workflow across a team. In production-first projects, consistency beats cleverness: everyone should run the same commands locally, in CI, and in deployment.

Core Workflow Scripts

A solid baseline for backend projects:

{
  "scripts": {
    "dev": "tsx watch src/index.ts",
    "build": "tsc -p tsconfig.json",
    "start": "node dist/index.js",
    "test": "node --test",
    "lint": "eslint ."
  }
}

dev vs start

  • dev: fast feedback, runs TypeScript directly (or watches)
  • start: production-like, runs compiled JavaScript

Mixing these leads to “works on my machine” problems.

Passing Arguments

You can pass args after --:

npm run test -- --watch

Pre and Post Scripts

npm supports lifecycle hooks. Example:

{
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "tsc",
    "postbuild": "node dist/healthcheck.js"
  }
}

Use these carefully. Hidden behavior can confuse teams.

Environment Variables in Scripts

Cross-platform issue: setting env vars differs between Windows and Unix shells.

Unix Style

PORT=3001 node dist/index.js

Windows CMD Style

set PORT=3001 && node dist/index.js

Production-Friendly Solution

Avoid shell-specific tricks. Prefer reading environment variables from the environment (set by your process manager, CI, or server config) rather than baking them into scripts.

Common Script Patterns

  • dev: run local server with watch
  • build: produce artifacts
  • start: run artifacts
  • test: run tests in CI
  • lint: enforce code quality

CI/CD Tip

In CI, prefer deterministic installs and explicit steps:

npm ci
npm run build
npm test

Production Insight

Keep scripts boring. Scripts are infrastructure. Complexity here becomes operational cost later.

Next Step

Next we will cover dependency management strategies: semver, updates, audits, and keeping your backend stable over time.