2016-05-10 37 views
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如何使用node.js從.json文件而不是命令行訪問參數?

Here is the source documentation for process.argv

但是,如果我的參數存儲在.json文件中,而不是單獨在命令行中輸入,該怎麼辦?有沒有辦法使用node.js訪問它們?

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

我想加載data.json作爲參數以某種方式爲我的js文件,我也有我的計算機上本地存儲。然後運行它。

app.js
var foo = "bar", 
    baz = "bat"; // Somehow, I need to import these arguments from data.json 
// Then do stuff with them... 

回答

0

你可以要求你的JSON文件,並使用所需的值設置你的本地值。

代碼將是這樣的:

var params = require('./data.json'); 
var foo = params.foo; 
var bac = params.baz; 
0

您可以:

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

var foo = json.foo; 
相關問題