Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!