SkillByAIOpen interactive version →

Lesson 9 / 30

npm Scripts

Wrap common commands in npm scripts such as start, test and dev, and run them with npm run.

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`?

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

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