-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
Description
Affected URL(s)
https://nodejs.org/api/process.html#processargv
Description of the problem
The api documentation for process.argv states:
The first element will be [
process.execPath][]. Seeprocess.argv0if access to the original value ofargv[0]is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command-line arguments.
This description is incomplete for no-script execution modes like -e/--eval (evaluating inline JavaScript).
When running Node.js with -e/--eval (e.g., node -e "console.log(process.argv[1])" "Hello world"), there is no "JavaScript file being executed". In reality, the command outputs "Hello world"—this behavior has been verified consistently on Windows 10 CMD and Linux Bash, proving process.argv[1] maps to the first unconsumed command-line argument, not a file path.
This behavior probably aligns with Node.js parsing logic (e.g., src/node.cc / src/node_options.h ):
-e/--evalis a value-required built-in option, it's classified asexec_args(internal to Node.js, not exposed toprocess.argv).- Unconsumed arguments (e.g.,
"Hello world") are added to theargsarray, which maps toprocess.argvin the JS layer.
Proposed fix:
Revise the process.argv description to:
The first element will be [
process.execPath][]. Seeprocess.argv0if access to the original value ofargv[0]is needed. The second element will be the path to the JavaScript file being executed (if a script file is provided). For no-script execution modes (e.g.,-e/--eval), the second element is the first unconsumed command-line argument. The remaining elements are additional command-line arguments.
This clarification may resolve confusion from the documentation and aligns it with actual runtime behavior.