Skip to content

Commit a804db1

Browse files
ppannutoaddaleax
authored andcommitted
process: save original argv[0]
For historical and other reasons, node overwrites `argv[0]` on startup. See - 2c6f79c, - #7434 - #7449 - #7696 For cases where it may be useful, save the original value of `argv[0]` in `process.argv0` PR-URL: #7696 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent beea23a commit a804db1

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

doc/api/process.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,10 @@ added: v0.1.27
457457

458458
The `process.argv` property returns an array containing the command line
459459
arguments passed when the Node.js process was launched. The first element will
460-
be [`process.execPath`]. The second element will be the path to the
461-
JavaScript file being executed. The remaining elements will be any additional
462-
command line arguments.
460+
be [`process.execPath`]. See `process.argv0` if access to the original value of
461+
`argv[0]` is needed. The second element will be the path to the JavaScript
462+
file being executed. The remaining elements will be any additional command line
463+
arguments.
463464

464465
For example, assuming the following script for `process-args.js`:
465466

@@ -486,6 +487,22 @@ Would generate the output:
486487
4: four
487488
```
488489

490+
## process.argv0
491+
<!-- YAML
492+
added: REPLACEME
493+
-->
494+
495+
The `process.argv0` property stores a read-only copy of the original value of
496+
`argv[0]` passed when Node.js starts.
497+
498+
```js
499+
$ bash -c 'exec -a customArgv0 ./node'
500+
> process.argv[0]
501+
'/Volumes/code/external/node/out/Release/node'
502+
> process.argv0
503+
'customArgv0'
504+
```
505+
489506
## process.chdir(directory)
490507
<!-- YAML
491508
added: v0.1.17

lib/internal/bootstrap_node.js

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050

5151
_process.setupRawDebug();
5252

53+
Object.defineProperty(process, 'argv0', {
54+
enumerable: true,
55+
configurable: false,
56+
value: process.argv[0]
57+
});
5358
process.argv[0] = process.execPath;
5459

5560
// There are various modes that Node can run in. The most common two

0 commit comments

Comments
 (0)