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

后端 未结 30 3102
梦如初夏
梦如初夏 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:28

    Several great answers here, but it all seems very complex. This is very similar to how bash scripts access argument values and it's already provided standard with node.js as MooGoo pointed out. (Just to make it understandable to somebody that's new to node.js)

    Example:

    $ node yourscript.js banana monkey
    
    var program_name = process.argv[0]; //value will be "node"
    var script_path = process.argv[1]; //value will be "yourscript.js"
    var first_value = process.argv[2]; //value will be "banana"
    var second_value = process.argv[3]; //value will be "monkey"
    

提交回复
热议问题