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.

Jon Koops
  • 8,801
  • 6
  • 29
  • 51
Michael Segal
  • 25
  • 1
  • 1
  • 5

1 Answers1

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

h1b9b
  • 1,010
  • 5
  • 16