NPM Scripts: Automate Your Workflow
Advertisement
Ad
What are NPM Scripts?
NPM scripts let you define custom commands in package.json to automate tasks like building, testing, and starting your app.
Defining Scripts
{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"build": "webpack",
"test": "jest",
"lint": "eslint ."
}
}
Running Them
npm start # special — no "run" needed
npm test # special
npm run dev # custom scripts need "run"
npm run build
Pre & Post Hooks
{
"scripts": {
"prebuild": "npm run clean", // runs before build
"build": "webpack",
"postbuild": "echo Done!" // runs after build
}
}
Chaining Scripts
"ci": "npm run lint && npm test && npm run build"
FAQs
Why "run" for some but not others?
start and test are built-in shortcuts; custom scripts need run. More in our NPM guides.
Can scripts call other scripts?
Yes — npm run other inside a script.
