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.
import { spawnSync } from 'node:child_process'; spawnSync(process.execPath, [...process.execArgv, 'child.mjs']);
Try it
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.
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
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.execPathto 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
execArgvon purpose. Debug flags like--inspectcopied into a child will make both processes try to bind the same port; filter them out when needed.worker_threadsandchild_process.fork()copyexecArgvautomatically. - Flags set through
NODE_OPTIONSdo not appear inexecArgv; readprocess.env.NODE_OPTIONSfor them. execArgvis notargv.process.argvholds the script and your arguments;execArgvholds only Node flags.-e,-pand REPL runs: no script exists, soargv[1]is not a file, butexecPathstill works.- Packaged apps. In a single executable application
execPathis the app itself, not a separatenode. - Windows paths end with
.exeand may contain spaces; pass them as an argument list (not a shell string) to avoid quoting problems.