npm Scripts & the Dev Workflow
7 min read
npm Scripts
The scripts field in package.json lets you define short commands that npm can run. This is how you automate starting servers, running tests, building assets, and more — without installing a separate task runner.
json
{
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"build": "tsc",
"lint": "eslint src/",
"test": "jest --coverage",
"prestart": "npm run build"
}
}Run any script with npm run <name>. The special scripts start, test, stop, and restart can be run without the run keyword: npm start, npm test, etc.
nodemon for Auto-Restart
nodemon watches your source files and restarts the Node process automatically when changes are detected. It is the standard dev-time tool for Node.js servers.
bash
npm install --save-dev nodemon
npm run dev
# [nodemon] starting `node server.js`
# Server running on port 3000
# [nodemon] restarting due to changes...You can configure nodemon with a nodemon.json file to watch specific extensions, ignore directories, or set environment variables.
Ready to test yourself?
Practice Node.js— quiz & coding exercises