[英]How to use node.js to access arguments from a .json file instead of command line?
This SO answer describes use of the process.argv
variable to access command line arguments when using node.js to run a javascript file . 这个SO答案描述了在使用node.js运行javascript文件时使用
process.argv
变量访问命令行参数 。
Here is the source documentation for process.argv
. 这是
process.argv
的源文档 。
But what if my arguments are stored in a .json file instead of input individually at the command line. 但是,如果我的参数存储在.json文件中,而不是在命令行中单独输入该怎么办? Is there a way to also access them using node.js?
有没有办法也可以使用node.js访问它们?
var foo = "bar",
baz = "bat"; // Somehow, I need to import these arguments from data.json
// Then do stuff with them...
I want to load data.json as arguments somehow into my .js file that I also have stored locally on my computer. 我想以某种方式将data.json作为参数加载到我的.js文件中,该文件也已存储在本地计算机上。 Then run it.
然后运行它。
var foo = "bar", baz = "bat"; // Somehow, I need to import these arguments from data.json // Then do stuff with them...
You can require your json file, and set your local values with required values. 您可以要求使用json文件,并使用必需的值设置本地值。
Code will be like this: 代码将如下所示:
var params = require('./data.json');
var foo = params.foo;
var bac = params.baz;
You can : 您可以 :
var json = require('youjsonfile.json');
var foo = json.foo;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.