Skip to content

Commit

Permalink
process: save original argv[0]
Browse files Browse the repository at this point in the history
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 <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
ppannuto authored and addaleax committed Aug 8, 2016
1 parent beea23a commit a804db1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
23 changes: 20 additions & 3 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,10 @@ added: v0.1.27

The `process.argv` property returns an array containing the command line
arguments passed when the Node.js process was launched. The first element will
be [`process.execPath`]. The second element will be the path to the
JavaScript file being executed. The remaining elements will be any additional
command line arguments.
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.

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

Expand All @@ -486,6 +487,22 @@ Would generate the output:
4: four
```

## process.argv0
<!-- YAML
added: REPLACEME
-->

The `process.argv0` property stores a read-only copy of the original value of
`argv[0]` passed when Node.js starts.

```js
$ bash -c 'exec -a customArgv0 ./node'
> process.argv[0]
'/Volumes/code/external/node/out/Release/node'
> process.argv0
'customArgv0'
```

## process.chdir(directory)
<!-- YAML
added: v0.1.17
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@

_process.setupRawDebug();

Object.defineProperty(process, 'argv0', {
enumerable: true,
configurable: false,
value: process.argv[0]
});
process.argv[0] = process.execPath;

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

0 comments on commit a804db1

Please sign in to comment.