Special Script Names
What you'll learn: Discover which npm script names have built-in shortcuts and how pre/post hooks automatically run alongside your scripts.
The Shortcut Scripts
Not all npm scripts require the npm run prefix. NPM reserves special names that you can invoke directly:
npm start→ runs thestartscriptnpm test(ornpm t) → runs thetestscriptnpm stop→ runs thestopscriptnpm restart→ runs therestartscript
These are so commonly used that NPM gives them VIP treatment. Instead of typing npm run start, you simply type npm start.
Pre and Post Hooks
NPM automatically runs "pre" and "post" versions of any script you define. Think of them as middleware that bookends your main task:
{
"scripts": {
"prebuild": "echo 'Cleaning old files...'",
"build": "webpack",
"postbuild": "echo 'Build complete!'"
}
}
When you run npm run build, NPM executes all three in order: prebuild → build → postbuild. You don't need to call the hooks manually—they trigger automatically.
This works for any script name, including custom ones:
{
"scripts": {
"predeploy": "npm run build",
"deploy": "scp -r dist/ server:/var/www",
"postdeploy": "echo 'Deployed successfully'"
}
}
Hooks are perfect for setup tasks (installing dependencies, clearing caches) or cleanup (notifications, logging).
Combining Both Features
Special scripts support hooks too:
{
"pretest": "eslint .",
"test": "jest",
"posttest": "echo 'Tests passed'"
}
Running npm test automatically triggers linting first, then tests, then confirmation.
Key Takeaway: Special script names like start and test don't need npm run, and any script can have pre/post hooks that run automatically before and after the main command.