Lesson 9 / 28

npm Scripts

Wrap common commands behind short names.

The scripts field

Define named commands in package.json, then run them with npm run.

{
  "scripts": {
    "start": "node index.js",
    "dev": "node --watch index.js",
    "test": "node --test"
  }
}

Running scripts

start and test are special — run them without run.

npm start        # runs "node index.js"
npm test         # runs "node --test"
npm run dev       # custom scripts need "run"

Quick check: Which two script names can you run without typing `run`?

  • start and test
  • dev and build
  • install and stop
Answer

start and test — `npm start` and `npm test` are shortcuts baked into npm; every other script name needs `npm run <name>`.