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

251
Visualizações
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 à 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