This code as it is opens the browser and read a txt file line by line:
line 1
line 2
line 3
Stream closed
The thing I don't understand is why placing const browser = await puppeteer.launch(...) before const stream = readline.createInterface(...) blocks the execution of the callbacks in stream.on(...), the brower get launched but the console doesn't display anything.
const fs = require("fs");
const readline = require("readline");
const puppeteer = require("puppeteer");
(async () => {
const filePath = "largefile.txt";
if (!fs.existsSync(filePath)) {
console.error(`File ${filePath} does not exist!`);
return;
}
const browser = await puppeteer.launch({ headless: false, slowMo: 50, devtools: true });
const stream = readline.createInterface({
input: fs.createReadStream(filePath),
output: process.stdout,
terminal: false,
});
//const browser = await puppeteer.launch({ headless: false, slowMo: 50, devtools: true });
stream.on("line", async (line) => {
console.log(line);
});
stream.on("close", () => {
console.log("Stream closed");
});
stream.on("error", (err) => {
console.error("Stream error:", err);
});
})();