Lifecycle scripts and workspaces
Local practice: Node.js 22.16.0 and npm 10.9.2 on Linux x64; proposed teaching baseline 26.13.1 LTS remains untested. Use a fresh disposable folder for each example, copy only the shown files and run the fixed command. No packages or external services required. Static examples are not ByteMind runner support. The archive is author-only; do not copy expected.json into the example directory.
Run a reviewed npm script
{ "name": "report-fixture", "version": "1.0.0", "private": true, "scripts": { "report": "node main.mjs" }}Run in this fixture directory:
npm --silent run reportExpected stdout:
catalog readyExpected exit status: 0. Expected stderr: empty. The explicit report script launches this local file. The silent flag suppresses npm headings, not the application output.
How it works
npm run report executes the named command; matching prereport and postreport scripts can run before and after it. npm adds local binary directories to script PATH, so reviewed tools need not be installed globally. npm exec and npx can obtain missing packages; do not run unknown names. Workspaces are declared in the root manifest, for example workspaces: ["packages/*"]; installation can link local workspace packages and a workspace-specific task can be selected with npm run test --workspace=<name>. Our fixtures execute only Node with fixed local source and inspect a workspace declaration; no workspace install, registry access or lifecycle hook execution is attempted.
Read a workspace declaration
const root = { private: true, workspaces: ["packages/catalog", "packages/cli"] };for (const workspace of root.workspaces) console.log(workspace + "/package.json");Run in this fixture directory:
node main.mjsExpected stdout:
packages/catalog/package.jsonpackages/cli/package.jsonExpected exit status: 0. Expected stderr: empty. This lists the expected manifest locations for two explicit workspace paths; it does not perform npm workspace discovery or linking.
Common mistake
Do not build a script command by concatenating untrusted input. Review pre/post hooks and install-time scripts before running tasks; a dependency or project can run code through them.
Try it yourself
Design a root project with packages/catalog and packages/cli. Identify where each package manifest belongs and how to select only the CLI test task.
Further reading: Official documentation 1, Official documentation 2. Related ByteMind reference candidate: node-npm-run (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Lifecycle scripts and workspaces — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
What may run before npm run report?
What can npm exec do when a binary is missing?
What does the workspaces field describe?