Node passes the parameter process.argv when executing the command.

The process object is a global variable that provides information about the current Node.js process and controls the current Node.js process. Because it is a global variable, there is no need to use require().

The process.argv property returns an array containing the command line arguments when starting the Node.js process.

in:

The first element of the array process.argv[0] - returns the absolute path to the executable file that started the Node.js process.

The second element process.argv[1] - is the path to the currently executed JavaScript file

The remaining elements are other command line parameters

For example:

Enter the command: node scripts/build.js "web-runtime-cjs,web-server-renderer"
Result:

console.log(process.argv[0]) // Print D:\nodeJs\node.exe
console.log(process.argv[1]) // Print E:\Study_document\vue-resource\vue-dev\scripts \build.js
console.log(process.argv[2]) // Print web-runtime-cjs,web-server-renderer

Author: whh666

Source: https://www.cnblogs.com/whh666/p/17482716.html

Copyright: This work is licensed under the " Attribution-Noncommercial-ShareAlike 4.0 International " license.

When node executes the command, pass the parameter process.argv

Terminal input:

node app.js --name=Xiaoming--age=13 test1 test2 123

The content of app.js is as follows:

method one:

console.log('process.argv', process.argv)

Print out the following content:
process.argv = [
  '/usr/local/bin/node',
  '/Users/xxx/Desktop/xxx/app.js',
  '--name=Xiao Ming',
  '--age=13 ',
  'test1',
  'test2',
  '123'

Method 2:
process.argv.forEach((val, index) => { console.log(`${index}: ${val}`); });

Print out the following:

0: /usr/local/bin/node
1: /Users/a123456/Desktop/EDAS Reconstruction/webpack0831/script/gitac.js
2: --name=Xiao Ming3
: --age=13
4: test1
5: test2
6:123

How to pass parameters process.argv + minimimist_process.argv when node executes a command_X.Py's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/luohaitao/article/details/132620942