How can I log messages from within page.evaluate using Puppeteer?

I’m trying to capture log output from within the page.evaluate function so that I can see those messages in my Node environment while the page is being processed. I need to monitor the progress of the page evaluation in real time and display specific output to the user during script execution. What is the best way to pass console messages from the browser context back to Node during evaluation?

The most robust method I found is binding an event listener with page.on(‘console’) in the Node environment. This way, every console message from within page.evaluate is intercepted nicely by Puppeteer. The event provides the type of log and its contents, so you can filter or directly pass on the messages as needed. This approach directly connects the browser’s console output to your Node process, making it easier to monitor progress and debug any issues while the evaluation is ongoing without altering your internal logic.