0

I'm writing a simple JS script which gets a file as input and does some manipulation to it:

node script.js file

The file part is accessed directly by using:

process.argv[2]

Now all i want is given that input, find that file and read it line by line into my function. I have a really hard time with this seemingly simple task since all solutions I found were HTML based.

I need this to run as an independent script.

2

1 Answer 1

1

You can do it using fs and readline:

const readline = require('readline');
const fs = require('fs');

const rl = readline.createInterface({
  input: fs.createReadStream(/* Path to your file */),
  crlfDelay: Infinity
});

rl.on('line', (line) => {
  console.log(`Line from file: ${line}`);
});

cf documentation for details, fs & readline

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

Comments

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.