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

219
Vistas
Why is the data not written to the file?

I have this code which is supposed to write data in text file:

const test = [
  'test1',
  'test2'
]
fs.writeFile('file.txt', test, {flag: "a"}, err => {
  if (err) throw err
})

The problem is that code works without errors, but there is no data in file. If I just put and execute this piece of code in some test file, it works correctly, but not in function, where this code is supposed to be. What's wrong? Why there is no data in file if I execute code in function, but everything works correctly, when I execute it in test file?

Here is the code of function:

const fs = require("fs");

(async () => {
    const test = [
      'test1',
      'test2'
    ]
    fs.writeFile('file.txt', test, {flag: "a"}, err => {
      if (err) throw err
    })

    process.exit(1);
  } catch (e) {
    console.log('error: ', e)
    process.exit(1)
  }
})()
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're missing the "try" part of your try/catch.

You're also exiting the script before the file is written.

try writeFileSync instead.

const fs = require("fs");

(async() => {
  const test = [
    'test1',
    'test2'
  ]
  try {
    fs.writeFileSync('file.txt', test, {
      flag: "a"
    });
    process.exit(1);
  } catch (e) {
    console.log('error: ', e)
    process.exit(1)
  }
})()
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