Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Puppeteer on WSL2

Is there a straightforward way to run Puppeteer on wsl2? I've tried many solutions across the web and read tons of forums looking for answers. So far no success and I've found running Puppeteer very easy in windows but extremely difficult in wsl2.

I've tried downloading chrome and chromium, and specifying the executablePath with no luck.

I know that it works on my windows system, but cannot seem to get Puppeteer to run in wsl2.

I am currently stuck on this error:

Timeout Error: Timed out after 30000 ms while trying to connect to the browser! Only Chrome at revision r1022525 is guaranteed to work.

When looking into node_modules I can see that version 1022525 is in use. I can't understand what Puppeteer's problem is nor how to fix it.

Anyone have any ideas?

like image 949
Robert Avatar asked Oct 18 '25 15:10

Robert


1 Answers

As I do not see exactly what are you trying to do , in the moment I can think that this error accused because of your windows defender firewall, which do not allow access of your X-server.

anyway for me I use Puppeteer on wsl2 as following:

  1. I need to install VcXsrc as X-server to display what is going on in the Linux side of the computer. https://sourceforge.net/projects/vcxsrv notes: you need to allow “Public networks” when the Windows firewall pops up.

  2. You need now to set a DISPLAY environment variable has to be set,becuase wsl has its own IP address, we need to connect to our X-server app using this IP so to do this follow the next steps.

  3. In your .bashrc (or .zshrc if you are using ZSH) add this line exactly to the end: export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0

  4. now executes the content of your bash use source ~/.bashrc (or source ~/.zshrc if you are using ZSH)

  5. to be sure that your DISPLAY environment variable has set correctly , try this command (echo $DISPLAY) the result should be something like this : # 172.16.210.1:0.0 it will be your wsl IP address.

  6. now Open the VcXsrv program in Windows (called XLaunch), allow this options step after step “Multiple windows” and “Start no client”. and in the end tick the option of disable access control Be sure to disable access control.

  7. as last check go to windows defender firewall and be sure that you enable all network connection for VcXSrv. now you are ready to go, let suppose I am using puppeteer to test inside jest file like the follwoing :

    const puppeteer = require('puppeteer'); test('should create an element...etc', async () => { const browser = await puppeteer.launch({ headless: false, args: ['--window-size=1920,1080'] }); const page = await browser.newPage(); await page.goto( 'www.google.com' ); });

start the last code using (jest nameofyourtestfile) and your puppeteer will work as you like, Chromium will open in your windows ...enjoy...if there is any details which is not clear may you ask again. I hope that I helped

like image 117
Barry Avatar answered Oct 22 '25 05:10

Barry