Node.js Reference ยท process
Node.js process.argv
Reference for process.argv in Node.js: the array of command-line arguments, what the first two entries are, how to parse flags and when to use util.parseArgs.
process.argv is an array of strings holding the command-line arguments the Node.js process was started with. The first element is the absolute path of the Node executable, the second is the absolute path of the script being run, and everything after that is what the user typed after the script name.
All values are strings, even when they look like numbers or flags. Node does not parse --verbose or --port=3000 for you; options that belong to Node itself (such as --watch) are removed before your script sees the list.
// node app.mjs build --fast const [, , command, ...rest] = process.argv;
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.argv: string[] process.argv0: string
| Name | Description |
|---|---|
argv[0] |
Absolute path of the executable that started the process (same as process.execPath). |
argv[1] |
Absolute path of the script file. For node -e or REPL runs there is no script entry. |
argv[2...] |
The arguments the user supplied, each as a string. |
process.argv0 |
A copy of the original argv[0] exactly as it was typed, for example node, before it was resolved. |
Returns: a mutable array of strings.
Edge cases: quoting is handled by your shell, not by Node, so "a b" arrives as one element. An empty argument arrives as an empty string.
Usage notes
- Skip the first two entries. The usual idiom is
process.argv.slice(2). - Everything is a string. Convert numbers yourself and check for
NaN. - Use
util.parseArgs()for flags. It handles--name value,--name=value, short flags, defaults and positionals, and throws on unknown options in strict mode (the default). - The
--separator. Arguments after--are passed through untouched, which is how you give options to your script rather than to a tool that wraps it. - Node's own options are removed.
node --watch app.mjs xgivesargvof node, app.mjs,x; the flags before the script are inprocess.execArgv. argv[1]is resolved. Runningnode appgives the full path of the file Node found, including the extension.- Do not trust the values. Arguments come from whoever starts the program; validate paths and numbers before using them.
npm runscripts pass extra arguments only after--, for examplenpm run build -- --fast.