npm scripts
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.
{ "name": "greeter", "version": "1.0.0", "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
> greeter@1.0.0 build
> tsc
npm prints the command it found; tsc itself adds nothing on a clean build.
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
> greeter@1.0.0 start
> node main.js 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
> greeter@1.0.0 build
> tsc
> greeter@1.0.0 start
> node main.js
Hello, stranger
No name follows the script here, so the program falls back to its default greeting.
This one line compiles and then runs in a safe order. It is a good default workflow while a project is still small.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the scripts map of package.json provide?
Answer
Short names for long terminal commands
A script name runs the quoted command, so every teammate uses the same flags.
-
What does
npm run buildprint before the command runs?Answer
The package name, the script name, and the command it found
npm shows the banner of the script, then the command output follows.
-
What does the
--separator do innpm run start -- Ada?Answer
It passes Ada to the script instead of npm reading it as an option
The words after the separator reach the underlying program argv.
-
What does
&&do between two commands?Answer
It runs the second command only when the first succeeds
A failed build stops the chain, so stale output never starts.
-
What does this program print?
const args = ["Ada"]; console.log(args[0] ?? "stranger");Answer
Ada
The array holds Ada, so the fallback is not used.
-
What does this program print?
console.log(["build", "start"].join(" && "));Answer
build && start
The join puts the separator between the two script names.
-
What does this program print?
function greet(name?: string): string { return "Hello, " + (name ?? "stranger"); } console.log(greet());Answer
Hello, stranger
The nullish operator returns the fallback for undefined.
-
What does this program print?
console.log(true && "ok");Answer
ok
The left side is true, so the expression returns the right side.
-
What does the compiler say about this program?
const name: string = undefined; console.log(name);Answer
error TS2322: Type 'undefined' is not assignable to type 'string'.
With strictNullChecks a string variable cannot hold undefined.
-
What does the compiler say about this program?
const words: string[] = "Ada"; console.log(words);Answer
error TS2322: Type 'string' is not assignable to type 'string[]'.
A plain string is not an array of strings.
Article author: Arthur Isaev