I’m working with Puppeteer and need to display console output from within the page.evaluate function. I want these messages to appear in my Node.js console while the page evaluation is happening.
Basically, I need to track what’s going on during the page.evaluate execution and show updates to the user. Is there a way to get console.log statements from inside page.evaluate to show up in my terminal?
I tried using regular console.log but it doesn’t seem to pass through to Node.js. Any suggestions on how to make this work?
heads up - console logging inside page.evaluate slows things down bad. message passing between browser and node creates overhead. learned this when my scraping got super slow from too many debug logs. use return statements for critical data and only console.log when debugging
Yeah, the console event listener approach works, but timing can bite you if you’re not careful. I always put the listener right after creating the page instance - not right before evaluate. Watch out for console message buffering. It varies by browser context, and async operations inside page.evaluate won’t always show up immediately in your terminal. Easy fix: add a small delay or use page.waitForTimeout after your evaluate call. Also, console.error and console.warn need separate handling if you want to tell them apart. The msg object has a type() method that gives you the console level - super handy for different formatting based on severity.
Claire’s solution works, but you can push this way further with automation. Skip manually handling console events every time - set up a workflow that captures browser interactions and logs everything automatically.
I built something like this for our QA team when we had to monitor hundreds of page evaluations daily. Manual console catching becomes a nightmare once you scale.
With automation, you capture console messages, grab screenshots on errors, save logs to files, and send alerts when specific patterns pop up. The workflow handles everything in sequence and catches edge cases you’d miss doing it by hand.
For what you need right now? Yeah, page.on(‘console’) works fine. But if you want something that runs the entire monitoring pipeline without writing a ton of boilerplate, go with automation.
You can set up browser automation workflows that handle Puppeteer operations with built-in logging and monitoring. Way cleaner than juggling event handlers manually.
Heads up - console messages from iframes or nested contexts won’t get captured by your main page listener. I found this out the hard way on a project where we were scraping pages with embedded widgets. Some logs just weren’t showing up, and it drove me crazy until I realized those widgets ran in separate contexts. You’ve got to listen for console events on each frame separately.
Also, if you’re running multiple page instances at once, make sure you bind the console listener to the right page object. I’ve seen devs accidentally attach all listeners to the same page reference and then wonder why they’re getting mixed output from different evaluations.
u can catch console messages with page.on(‘console’, msg => console.log(msg.text())) - just put it before your page.evaluate. any console.log from the page will show up in ur terminal, makes debugging much easier!