npm Scripts Guide
npm scripts are the standard way to run tasks in Node.js projects. Use the generator above to quickly set up scripts for your project, then customize them here.
Basic Info
Module Config
{
"name": "my-nextjs-app",
"version": "0.1.0",
"private": true,
"type": "module",
"main": "index.js",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "^15.0.0",
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"devDependencies": {
"typescript": "^5.7.0",
"@types/node": "^22.0.0",
"@types/react": "^19.0.0",
"@types/react-dom": "^19.0.0",
"eslint": "^9.0.0",
"eslint-config-next": "^15.0.0"
},
"license": "MIT"
}4
Scripts
3
Deps
6
DevDeps
Press Ctrl+Enter to copy
Built-in lifecycle scripts
npm has special script names that run automatically: 'preinstall' and 'postinstall' run before/after npm install, 'prepublishOnly' runs before npm publish (ideal for building), 'prepare' runs after install and before publish. These hooks automate common workflows without manual intervention.
// package.json — common npm scripts
{
"scripts": {
"dev": "next dev", // development server
"build": "next build", // production build
"start": "next start", // start production
"lint": "eslint . --fix", // lint + auto-fix
"test": "vitest", // run tests
"test:watch": "vitest --watch", // watch mode
"typecheck": "tsc --noEmit", // type checking only
"format": "prettier --write .", // format code
"prepare": "husky install", // git hooks setup
"precommit": "lint-staged" // pre-commit checks
}
}
// Run: npm run dev, npm test, npm run build
// Pre/post hooks: "prebuild" runs before "build" automaticallyPre and post hooks
Any script can have pre/post hooks. 'prebuild' runs before 'build', 'postbuild' runs after. This lets you chain tasks: prelint → lint → postlint. Common pattern: 'prebuild' cleans the dist folder, 'build' compiles, 'postbuild' runs tests. Pre/post hooks run automatically — you just run 'npm run build'.
Script composition patterns
Run scripts in sequence with '&&': "build": "tsc && esbuild ...". Run in parallel with packages like concurrently or npm-run-all: "dev": "concurrently \"tsc -w\" \"node server.js\"". Reference other scripts: "ci": "npm run lint && npm run test && npm run build".
Cross-platform compatibility
Avoid bash-specific syntax in scripts — Windows users can't run 'rm -rf dist'. Use cross-env for environment variables, rimraf for deletion, and cpy-cli for copying. Or use Node.js scripts directly: "clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"".
Frequently Asked Questions
What is the difference between npm run and npx?
'npm run <script>' executes a script defined in package.json. 'npx <command>' runs a package's binary directly, downloading it temporarily if not installed. Use npm scripts for repeatable project tasks, npx for one-off commands.
Can I pass arguments to npm scripts?
Yes, use -- to separate npm's arguments from the script's arguments: 'npm run test -- --watch' passes --watch to the test command. In npm 7+, you can also use 'npm test -- --watch' for built-in scripts.
Should I use npm scripts or a task runner like Gulp?
npm scripts are sufficient for most projects and add no extra dependencies. Task runners like Gulp add complexity but offer streaming pipelines and plugin ecosystems. For new projects, start with npm scripts and only add a task runner if you hit limitations.