Chapter 18: Node NPM Scripts

1. What are npm scripts really?

npm scripts are custom commands you define inside the package.json file under the key “scripts”.

Instead of typing long commands every day, you give them short, memorable names and run them with:

Bash

or special shortcuts:

Bash

Why are they so important?

  • They make your project portable — anyone who clones your repo can run the same commands
  • They hide complexity (long flags, environment variables, chained tools…)
  • They become the single source of truth for how to develop, build, test, lint, format, etc.
  • Almost every serious Node.js / JavaScript project lives and dies by its scripts section

2. Basic syntax – the minimal example

JSON

Run them like this:

Bash

Special shortcuts (you don’t need run):

  • npm start → runs “start” script
  • npm test → runs “test” script
  • npm restart → runs “restart” or stop + start
  • npm stop → runs “stop” script

3. Real-world npm scripts – typical patterns in 2026

Here’s a modern, realistic scripts section you see in many mid-to-large projects:

JSON

4. Most popular real patterns & explanations

Pattern 1 – Development server (hot reload)

JSON

or with tsx + dotenv:

JSON

tsx watch is very popular in 2025–2026 because:

  • Native TypeScript support
  • Fast reload
  • No need for separate compilation step

Pattern 2 – Build commands (TypeScript → JavaScript)

JSON

Common combinations:

  • tsc (official TypeScript compiler) + tsup / esbuild / vite / rollup
  • tsc –noEmit only for type checking

Pattern 3 – Linting & formatting

JSON

Many teams combine them:

JSON

Pattern 4 – Testing

JSON

Vitest is currently one of the most popular choices in 2026 (very fast, Vite-powered)

Pattern 5 – Database / migration scripts

JSON

Very common in Prisma projects

Pattern 6 – Pre & Post hooks (automatic tasks)

JSON

5. Useful tricks & best practices

Run multiple commands in one script

JSON

Use cross-env for environment variables (cross-platform)

JSON

Pass arguments to scripts

Bash

Use environment variables inside scripts

JSON

Best practice naming conventions

  • dev, start, build, test, lint, format → almost universal
  • Prefixes: db:, test:, build:, lint:, typecheck:
  • Suffixes: :watch, :fix, :check, :coverage, :ui

Summary – Quick cheat sheet of common scripts

JSON

Would you like to go deeper into any of these areas?

  • Creating advanced chained workflows (lint → format → test → build)
  • How to use environment variables inside scripts safely
  • Setting up husky + lint-staged (pre-commit hooks)
  • Difference between npm run vs npx vs direct binary calls
  • Best scripts for TypeScript + ESM + Vite / tsup / esbuild projects
  • Scripts for CI/CD (GitHub Actions, Vercel, Render…)

Just tell me which direction feels most useful right now — I’ll continue with concrete examples and

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *