Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
Useing fs.createReadStream and fs.createWriteStream for multiple files

I'm trying to loop over multiple files with fs.createReadStream, and I cannot figure out how to read the second file.

const fs = require('fs')
const csv = require('csv-parse')
const parser = csv({
  ...
})

const files = process.argv.slice(2)

async function analyzeFiles () {
  for (const file of files) {
    const string = file
    console.log(`Analyzing ${file}.`)
    await runFile(file, string)
    console.log(`Analyzed ${file}.`)
  }
}

async function runFile (filepath, string) {
  return new Promise(function (resolve, reject) {
    const shimmedData = {}
    let fileName = ''

    fs.createReadStream(filepath)
      .pipe(parser)
      .on('data', (row) => {
        // ...
        fileName = 'something dynamic from row'
        shimmedData[index] = row // Or something similar, not sure this matters
      })
      .on('error', (e) => {
        console.log('BONK', e)
      })
      .on('end', () => {
        
        fs.writeFile(`${fileName}.json`, JSON.stringify(shimmedData), (err) => {
          if (err) {
            console.log(err)
            reject(err)
          } else {
            console.log('File written successfully.')
            resolve()
          }
        })
      })
  })
}

analyzeFiles()

And then I run node script.js file1.txt file2.txt file3.txt

When I run this, only the first file will ever be saved. Looking into it with console logs, it looks like for the second file, fs.createReadStream is never called.

What am I missing?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!