npm scripts, arguments and exit status
Quick answer
npm scripts give a project named, repeatable commands. npm run executes a configured command, while its exit status communicates success or failure to automation.
Tooling examples
Example 1. Context: projects/npm-lab. Run in a fresh prepared fixture copy; see its README. Local check available; consult validation evidence for what was asserted.
npm run --silent label -- notebookExpected observation: notebook
Example 2. Context: projects/npm-lab. Run in a fresh prepared fixture copy; see its README. Local check available; consult validation evidence for what was asserted.
npm run --silent failExpected observation: Nonzero exit status.
How the tooling works
Project scripts can invoke locally installed binaries without hard-coded global paths. npm run name -- arguments forwards arguments to the named script. preNAME and postNAME can run around NAME; inspect these hooks before executing unfamiliar projects. npm uses a shell to run script strings, so avoid concatenating untrusted input into shell commands. Use an explicit failing exit status for test failure; do not hide it with an unconditional success command.
Common mistakes
--ignore-scripts for install does not mean an explicitly requested npm run script is safe. Review it first.
Practice
Run the echo script with two different labels. Run the intentionally failing script and inspect its exit status in the terminal.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
npm scripts, arguments and exit status — Knowledge Check
Assess tooling contracts, diagnostic reasoning and transfer to a project.
What separates npm options from arguments passed to a named npm run script?
What should a failed test command communicate to CI?
Why inspect preinstall hooks in unfamiliar dependencies?
In npm scripts, arguments and exit status, what is the expected observation for example 1 in its documented setup? npm run --silent label -- notebook
In npm scripts, arguments and exit status, what is the expected observation for example 2 in its documented setup? npm run --silent fail