How do I pass command line arguments to a Node.js program?

后端 未结 30 3106
梦如初夏
梦如初夏 2020-11-22 04:03

I have a web server written in Node.js and I would like to launch with a specific folder. I\'m not sure how to access arguments in JavaScript. I\'m running node like this:

30条回答
  •  孤城傲影
    2020-11-22 04:23

    Passing,parsing arguments is an easy process. Node provides you with the process.argv property, which is an array of strings, which are the arguments that were used when Node was invoked. The first entry of the array is the Node executable, and the second entry is the name of your script.

    If you run script with below atguments

    $ node args.js arg1 arg2
    

    File : args.js

    console.log(process.argv)
    

    You will get array like

     ['node','args.js','arg1','arg2']
    

提交回复
热议问题