3

I can't for the life of me think of a way to do this. I've previously worked with importing csv files into js files, but for this challenge I've got to create a js file that executable from a shell with the data file passed as input.

Any ideas how can this be done?

~The challenge description~

The program must be executable from a shell and take the CSV file as input. For example:

node score_calculator.js data.csv
5
  • Where do you get stuck ? Commented Nov 16, 2017 at 11:58
  • Well I'm fine writing the js file, but I'm not sure how data.csv will be passed to the js file Commented Nov 16, 2017 at 12:00
  • @user6456392 you should change your question to passing csv file as command like argument in nodeJs Commented Nov 16, 2017 at 12:15
  • Thanks @UsmanRana for the title suggestion 🙌. I've updated it! Commented Nov 16, 2017 at 14:32
  • @user6456392 you can accept and upvote the answer if this resolved your query :) Commented Nov 16, 2017 at 14:35

2 Answers 2

2

You can take the file path as command line argument when running your nodejs app like

node myScript.js pathToCsvFile

Now you'll have to get this path in your code as

var filePath = process.argv[2];

Now you can use this path to read your file as

   fs.readFile(filePath, (err, data) => {
     if (err) throw err;
    console.log(data);
 });

Read more about file handling in nodejs here

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

1

Sounds like you are trying to figure out how to pass and read parameters

You could do node score_calculator.js data.csv

then in your js

const csv = process.argv[2];

However. If you are passing in parameters I would recommend using minimist then you can do

node score_calculator.js --file=data.csv

And you js file would be

const argv = require('minimist')(process.argv.slice(2));
const csv = argv.file;

1 Comment

Thank you for the answer, but for the challenge I can't use any external modules. Minimist does seem really useful though 🙌

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.