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

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

    2018 answer based on current trends in the wild:


    Vanilla javascript argument parsing:

    const args = process.argv;
    console.log(args);
    

    This returns:

    $ node server.js one two=three four
    ['node', '/home/server.js', 'one', 'two=three', 'four']
    

    Official docs


    Most used NPM packages for argument parsing:

    Minimist: For minimal argument parsing.

    Commander.js: Most adopted module for argument parsing.

    Meow: Lighter alternative to Commander.js

    Yargs: More sophisticated argument parsing (heavy).

    Vorpal.js: Mature / interactive command-line applications with argument parsing.

提交回复
热议问题