Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda