Skip to content

doc: clarify process.argv[1] behavior for -e/--eval #61363

@ldlsn1

Description

@ldlsn1

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][]. See process.argv0 if access to the original value of argv[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 ):

  1. -e/--eval is a value-required built-in option, it's classified as exec_args (internal to Node.js, not exposed to process.argv).
  2. Unconsumed arguments (e.g., "Hello world") are added to the args array, which maps to process.argv in the JS layer.

Proposed fix:
Revise the process.argv description to:

The first element will be [process.execPath][]. See process.argv0 if access to the original value of argv[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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions