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

259
Vistas
Why output is blank when displaying return value of readFileSync on a file that is on a different dir, in NodeJS?

The code below is for reading the contents of a file, info2.txt which stored in a separate directory called data. The data directory is in the same place as the js file which contains the code, hence the filepath I have as argument to readFileSync().

The console displays a blank output. I have tried transferring the .txt file to the same dir as the js file and it was able to display, and have also tried using the async alternative readdir(). But I need it to be in another directory, and it needs to be synchronous, as part of an exercise's instruction.

const fs = require('fs');
const path = require('path');
try {
    
    const readStream = fs.createReadStream('./info.txt', 'utf8');
    const writeStream = fs.createWriteStream('./data/info2.txt', 'utf8');
    readStream.pipe(writeStream);
    
    
        let file = fs.readFileSync( './data/info2.txt', 'utf8');
        console.log(file.toString());

        
} catch (error) {
    if (error) console.log(error);
}
about 4 years ago · Juan Pablo Isaza
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