liveBook · Manning

Npm script

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

An npm script is a named command configuration in a project's package.json file that can be invoked through npm.

An npm script gives a project a memorable command for starting an application, running tests, or using a development tool. Define the command under `scripts` in `package.json`. Callers can then use the project’s command without knowing its entry-point filename or tool-specific arguments. section 2.6.7 section 9.5.8

Start a microservice

Set `"start": "node src/index.js"` in `package.json`, install dependencies with `npm install`, then run `npm start`. For a production-only dependency installation, use `npm install --omit=dev` instead. section 2.6.7

Use `npm run` for a custom script name. For example, `"start:dev": "nodemon src/index.js"` supports live reloading when nodemon is installed as a development dependency; invoke it with `npm run start:dev`. Unlike `start`, `start:dev` is a project convention, not a special npm command. section 2.6.8

Testing follows the same pattern: map `test` to Jest and run `npm test`. A separate `test:watch` script can run `jest --watchAll` through `npm run test:watch`. Another project can map `test` to Playwright instead, preserving `npm test` while changing the underlying tool. The Playwright setup shown does not provide a watch-mode script. section 9.5.8 section 9.8.6

Listing 2.7 Adding a start script for development
{
  "name": "example-3",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "start": "node src/index.js",         #1
    "start:dev": "nodemon src/index.js"   #2
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^5.0.0-beta.1"
  },
  "devDependencies": {                    #3
    "nodemon": "^2.0.18"                  #4
  } 
}

A complete `package.json` example places the standard start command and a nodemon-based development command alongside their respective dependencies. section 2.6.8

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Shows package.json entries and invocation commands for production, development, and testing, without examining npm’s execution internals.
Examples
Uses `npm start`, `npm run start:dev`, `npm test`, and `npm run test:watch` with matching package.json entries.
What it assumes
Assumes a Node.js project with package.json and teaches script invocation after installing the project’s dependencies.
What you can do after
Operate/configure it by adding named package.json commands and invoking standard or custom scripts through npm.
How it connects
Connects package.json, development dependencies, nodemon live reloading, and automated testing with Jest and Playwright.

Also explained in

Compare how 4 books explain npm script →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.