I'm new to programming in javascript. I am given a task to write a .js file that will run some input.json. I'm supposed to run it as such:
./name.js input.json
How can I incorporate node.js in this process and how do I get terminal to accept that script?
Thanks!
Edit: I solved my problem! I can't answer my own problem yet because of rules, anyway...
#!/usr/bin/env node
var fs = require('fs');
args = []
process.argv.forEach(function (val, index, array)
{
args.push(val);
});
var file = fs.readFileSync(args[2], "UTF-8", function (err, data)
{
if (err) throw err;
});
This is essentially what I did. I spent some time searching and bining things I found from different posts and got this to work - maybe it's not the best way, but it works. This stores my .json file into the file variable, which then I passed in as a function argument elsewhere. Thanks everyone.
I'm new to programming in javascript. I am given a task to write a .js file that will run some input.json. I'm supposed to run it as such:
./name.js input.json
How can I incorporate node.js in this process and how do I get terminal to accept that script?
Thanks!
Edit: I solved my problem! I can't answer my own problem yet because of rules, anyway...
#!/usr/bin/env node
var fs = require('fs');
args = []
process.argv.forEach(function (val, index, array)
{
args.push(val);
});
var file = fs.readFileSync(args[2], "UTF-8", function (err, data)
{
if (err) throw err;
});
This is essentially what I did. I spent some time searching and bining things I found from different posts and got this to work - maybe it's not the best way, but it works. This stores my .json file into the file variable, which then I passed in as a function argument elsewhere. Thanks everyone.
Share Improve this question edited Oct 28, 2012 at 7:09 minzeycat asked Oct 28, 2012 at 1:11 minzeycatminzeycat 1431 gold badge1 silver badge5 bronze badges 3-
2
You need a she-bang line, pointing to node.js.
#!/usr/bin/env node
perhaps – Anirudh Ramanathan Commented Oct 28, 2012 at 1:14 - possible duplicate of Is it possible to run Node.js scripts without invoking `node`? – Matt Ball Commented Oct 28, 2012 at 1:15
- @MattBall Those two questions don't look like duplicates – Anirudh Ramanathan Commented Oct 28, 2012 at 1:15
3 Answers
Reset to default 11 #!/usr/bin/env node
var mandLineArguments = process.argv.slice(2);
...
Maybe too obvious, but the easiest way is:
node ./name.js input.json
Maybe a bit late. But your post inspired me. I didn't knew that you can use js for an application. I always did it with shell script. The solution with the she-bang pointing to nodejs is awesome.
Thanks for the suggestion! I ended up doing something similar. When I did console.log(mandLineArguments); it gave me [ 'input.json' ], which isn't exactly what I wanted.
Now I can answer your ment. It gives you [ 'input.json' ]
right ? the []
tells you that it's an array and 'input.json'
is the only item. So you have to select the first element of that array.
console.log(mandLineArguments[0]);
I hope I could help you even when it's too late.
Thank you :D