How can you pass command line arguments to the Node.js?

Discussion in 'Web Design and Development' started by sarahjoness, Aug 25, 2015.

  1. sarahjoness macrumors newbie

    Joined:
    Dec 15, 2014
    Location:
    California
    #1
    How can you pass command line arguments to the Node.js?
     
  2. 960design macrumors 68020

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #2
    Just as you would pass command line arguments to most other applications:

    $ test.js mary had a little lamb

    The arguments can be handled within your script through the process.argv[] array.
    From your script it would look something like:

    var variableNameTwo = process.argv[1]; // set to 'mary'
    var variableNameFour = process.argv[3]; // set to 'a'
     
  3. steve-p macrumors 68000

    steve-p

    Joined:
    Oct 14, 2008
    Location:
    Newbury, UK
    #3
    I use nconf:

    https://github.com/indexzero/nconf

    It allows you to combine command line arguments with other strategies, such as configuration files and environment variables.
     

Share This Page