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

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

    To normalize the arguments like a regular javascript function would receive, I do this in my node.js shell scripts:

    var args = process.argv.slice(2);
    

    Note that the first arg is usually the path to nodejs, and the second arg is the location of the script you're executing.

提交回复
热议问题