Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
Node readFile inside a Loop Memory overflow

I have a folder with 98 .csv files that I need to read and save on a Postgres database. I'm running this code:

// Files in folder
const filesInFolder = fs.readdirSync('../pages-csv');

for (let i = 0; i < filesInFolder.length; i++) {
    console.log(`Saving file ${filesInFolder[i]}`)
    await fs.promises.readFile(path.join(__dirname, `../pages-csv/${filesInFolder[i]}`), 'utf8').then(async (data) => {
        await createOne(data) // database function
    })
}

But it leads to this error:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

It seems like files are not closing or not being flushed from memory after inserting in database.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Have you tried streaming it? Something like:

var csv = require('csv-parser')
var data = []

for (let i = 0; i < filesInFolder.length; i++) {
    console.log(`Saving file ${filesInFolder[i]}`)
    enter code here

    // add the 'file path here'
    fs.createReadStream(filesInFolder[i])
      .pipe(csv())
      .on('data', function (row) {
        createOne(row)
      })
      .on('end', function () {
       console.log('End cvs')
      })
}




Or maybe there is a way to clear the garbage collection: global.gc(), after you expose it: node --expose_gc

about 4 years ago · Juan Pablo Isaza Relatório

0

I found the problem, it was the createOne function, it had a forEach loop, removed it and now it works!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda