Skip to content

src: add --enable-run-hooks for node --run pre scripts#63991

Open
anonrig wants to merge 1 commit into
nodejs:mainfrom
anonrig:run-enable-hooks
Open

src: add --enable-run-hooks for node --run pre scripts#63991
anonrig wants to merge 1 commit into
nodejs:mainfrom
anonrig:run-enable-hooks

Conversation

@anonrig

@anonrig anonrig commented Jun 18, 2026

Copy link
Copy Markdown
Member

Summary

node --run intentionally does not run lifecycle scripts. This adds an
opt-in --enable-run-hooks flag that, together with --run, also runs the
matching pre<script> hook (when present in the package.json "scripts"
object) before the requested script.

  • The hook runs first; if it exits non-zero, the run is aborted and the main
    script does not execute.
  • Mirroring npm, positional arguments after -- are forwarded to the requested
    script only, not the hook.
  • Missing hooks, non-string hook values, and a missing main script are all
    handled without running anything spurious.
  • Without the flag, --run never runs hooks — existing behavior is
    unchanged.

Only the pre hook is supported (no post), keeping the feature minimal and
opt-in; the docs' "intentional limitations" note is updated accordingly.

Example

$ node --run build --enable-run-hooks   # runs `prebuild`, then `build`
$ node --run build                      # runs `build` only (unchanged)

Implementation

  • New --enable-run-hooks per-process option (src/node_options.{h,cc}).
  • task_runner::RunTask takes a run_hooks flag; it resolves pre<command_id>
    up front (copying the command and rewinding the simdjson object so the main
    lookup is untouched), runs it via a ProcessRunner, and aborts on failure.
  • src/node.cc passes cli_options->enable_run_hooks through.
  • Docs: doc/api/cli.md + doc/node.1.

Test plan

New test/parallel/test-node-run-hooks.js with a cross-platform fixture
(test/fixtures/run-script/hooks/, echo-based so it works on both sh and
cmd.exe), covering:

  • hook runs before the script (and in order) with the flag
  • hook does not run without the flag
  • failing hook aborts the script (exit 1, main output absent)
  • failing hook ignored when hooks disabled
  • positional args reach the script only, not the hook
  • missing hook → script still runs
  • non-string hook value is ignored
  • existing test-node-run.js / node_run_non_existent.js still pass
  • cpplint clean, test-cli-node-print-help passes
  • CI (make lint-js / lint-md + full matrix)

Made with Cursor

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/config
  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jun 18, 2026
node --run intentionally does not run lifecycle scripts. Add an opt-in
--enable-run-hooks flag that, together with --run, also runs the
matching "pre<script>" hook (when present in the package.json
"scripts" object) before the requested script.

The hook is run first via a ProcessRunner; if it exits with a non-zero
status the run is aborted and the main script is not executed. Mirroring
npm, positional arguments after `--` are forwarded to the requested
script only, not to the hook. Missing hooks, non-string hook values, and
a missing main script are all handled without running anything spurious.
Without the flag, --run never runs hooks, so existing behavior is
unchanged.

Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@anonrig anonrig force-pushed the run-enable-hooks branch from 123299a to b782130 Compare June 18, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants