npm scripts
| The scripts map | |
| npm run | |
| Pass args with -- | |
| Chain with && |
The scripts map
The scripts map gives short names to long terminal commands. Teammates then run the same build with the same flags, with no need to memorize the full text.
{
"scripts": {
"build": "tsc",
"start": "node main.js"
}
}
Here build means run the compiler, and start means run the output. Names stay short, while the quoted commands hold the exact behavior.
npm run
Run a saved script by name with npm run. The tool looks up the map, prints the command it found, and then executes it.
npm run build
compiled output is ready
The build above runs tsc with no extra typing. Short names remove friction, so builds happen often instead of rarely.
Pass args with --
Extra words after a double dash reach the underlying script. Without the separator, npm would try to read the words as its own options.
npm run start -- Ada
Hello, Ada
The name Ada flows through npm into the program argv array. Use this form whenever a script needs a name, a port, or a mode flag.
Chain with &&
The double ampersand chains two commands so the second runs only when the first succeeds. A failed build therefore never starts stale output.
npm run build && npm run start
Hello, TypeScript
This one line compiles and then runs in a safe order. It is a good default workflow while a project is still small.
Next: argv and env
Article author: Arthur Isaev