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

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

    You can reach command line arguments using system.args. And i use the solution below to parse arguments into an object, so i can get which one i want by name.

    var system = require('system');
    
    var args = {};
    system.args.map(function(x){return x.split("=")})
        .map(function(y){args[y[0]]=y[1]});
    

    now you don't need to know the index of the argument. use it like args.whatever

    Note: you should use named arguments like file.js x=1 y=2 to use this solution.

提交回复
热议问题