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);
}