I run app.js with mand node app.js
It executes const inputData = require('./input.json');
Is it possible pass file name as argument to const inputData = require('./file.json');
from mand line? I mean:
node app.js file.json
I am totally new to this trickery, have no theoretical point. From where I should start? Many thanks for all possible help.
Much obliged,
I run app.js with mand node app.js
It executes const inputData = require('./input.json');
Is it possible pass file name as argument to const inputData = require('./file.json');
from mand line? I mean:
node app.js file.json
I am totally new to this trickery, have no theoretical point. From where I should start? Many thanks for all possible help.
Much obliged,
Share Improve this question edited Nov 12, 2017 at 17:09 o.O asked Nov 12, 2017 at 17:04 o.Oo.O 5011 gold badge11 silver badges27 bronze badges 2-
1
you have
process.argv
to access cmdline arguments stackoverflow./a/4351548/3410584. Then simplyrequire
with the good index – ValLeNain Commented Nov 12, 2017 at 17:12 - Possible duplicate of How do I pass mand line arguments? – insert_name_here Commented Nov 12, 2017 at 17:30
1 Answer
Reset to default 5You can use the process.argv to access arguments, and fs.readFile or fs.readFileSync to read file content.
const fs = require('fs');
// Non-blocking example with fs.readFile
const fileNames = process.argv.splice(2);
fileNames.forEach(fileName => {
fs.readFile(fileName, 'utf-8', (error, data) => {
if (error) throw error;
console.log(fileName, data);
});
});
// Blocking example with fs.readFileSync
const fileName = fileNames[0];
console.log(fileName, fs.readFileSync(fileName, 'utf-8'));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745147554a4613715.html
评论列表(0条)