diff --git a/src/node.cc b/src/node.cc index 4ba019ddca05f4..4ce9603ac227d0 100644 --- a/src/node.cc +++ b/src/node.cc @@ -993,6 +993,13 @@ static ExitCode InitializeNodeWithArgsInternal( exit_code = ProcessGlobalArgsInternal(argv, exec_argv, errors, kDisallowedInEnvvar); if (exit_code != ExitCode::kNoFailure) return exit_code; + + auto env_opts = per_process::cli_options->per_isolate->per_env; + if (env_opts->watch_mode && !env_opts->test_runner && + env_opts->watch_mode_paths.empty() && argv->size() < 2) { + errors->push_back("--watch requires specifying a file"); + return ExitCode::kInvalidCommandLineArgument; + } } // Set the process.title immediately after processing argv if --title is set. diff --git a/src/node_options.cc b/src/node_options.cc index ca12586e479b99..c05bb3742cf331 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -266,8 +266,6 @@ void EnvironmentOptions::CheckOptions(std::vector* errors, } else if (test_runner_force_exit) { errors->push_back("either --watch or --test-force-exit " "can be used, not both"); - } else if (!test_runner && watch_mode_paths.empty() && argv->size() < 1) { - errors->push_back("--watch requires specifying a file"); } #ifndef ALLOW_ATTACHING_DEBUGGER_IN_WATCH_MODE diff --git a/test/sequential/test-watch-mode-watch-flags.mjs b/test/sequential/test-watch-mode-watch-flags.mjs index 385f381ad6a0ed..91a96d5269c51b 100644 --- a/test/sequential/test-watch-mode-watch-flags.mjs +++ b/test/sequential/test-watch-mode-watch-flags.mjs @@ -51,6 +51,15 @@ async function runNode({ tmpdir.refresh(); describe('watch mode - watch flags', { concurrency: !process.env.TEST_PARALLEL, timeout: 60_000 }, () => { + it('should require a file to watch', async () => { + const { code, signal, stderr, stdout } = await common.spawnPromisified(execPath, ['--watch']); + + assert.strictEqual(code, 9); + assert.strictEqual(signal, null); + assert.match(stderr, /--watch requires specifying a file/); + assert.strictEqual(stdout, ''); + }); + it('when multiple `--watch` flags are provided should run as if only one was', async () => { const projectDir = tmpdir.resolve('project-multi-flag'); mkdirSync(projectDir);