Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท process

Node.js process.execPath and process.execArgv

Reference for process.execPath and process.execArgv in Node.js: find the node binary, see the flags it started with, and spawn child Node processes correctly.

process.execPath is the absolute path of the Node.js executable that is running your program. process.execArgv is an array of the Node-specific command-line options that were given before the script name, such as --enable-source-maps or --max-old-space-size=256.

Both matter when you start another Node process from your own: use process.execPath as the program (instead of relying on node being on PATH) and pass process.execArgv so the child gets the same runtime flags.

NODE
import { spawnSync } from 'node:child_process';
spawnSync(process.execPath, [...process.execArgv, 'child.mjs']);

Try it

Node.js Inspecting the binary and flags

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Starting a child Node process

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
process.execPath: string
process.execArgv: string[]
process.argv0: string
Property Description
execPath Absolute path of the executable file. Symbolic links are kept as the system reports them.
execArgv Options meant for Node itself, in the order given, before the script. Excludes the script and your own arguments.
argv0 The original argv[0] as typed, for example node.

Returns: a string and an array of strings.

Usage notes

  • Use process.execPath to spawn Node. It guarantees that the child runs on the same version, even when several are installed or when the program is embedded in a single-file executable.
  • Pass execArgv on purpose. Debug flags like --inspect copied into a child will make both processes try to bind the same port; filter them out when needed. worker_threads and child_process.fork() copy execArgv automatically.
  • Flags set through NODE_OPTIONS do not appear in execArgv; read process.env.NODE_OPTIONS for them.
  • execArgv is not argv. process.argv holds the script and your arguments; execArgv holds only Node flags.
  • -e, -p and REPL runs: no script exists, so argv[1] is not a file, but execPath still works.
  • Packaged apps. In a single executable application execPath is the app itself, not a separate node.
  • Windows paths end with .exe and may contain spaces; pass them as an argument list (not a shell string) to avoid quoting problems.