0

This SO answer describes use of the process.argv variable to access command line arguments when using node.js to run a javascript file.

Here is the source documentation for process.argv.

But what if my arguments are stored in a .json file instead of input individually at the command line. Is there a way to also access them using node.js?

data.json
{ 
  "foo": "bar",
  "baz": "bat"
}

I want to load data.json as arguments somehow into my .js file that I also have stored locally on my computer. Then run it.

app.js
var foo = "bar",
    baz = "bat"; // Somehow, I need to import these arguments from data.json
// Then do stuff with them...
Community
  • 1
  • 1
Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207

2 Answers2

0

You can require your json file, and set your local values with required values.

Code will be like this:

var params = require('./data.json');
var foo = params.foo;
var bac = params.baz;
Ygalbel
  • 5,214
  • 1
  • 24
  • 32
0

You can :

var json = require('youjsonfile.json');

var foo = json.foo;
Jose Hermosilla Rodrigo
  • 3,513
  • 6
  • 22
  • 38