Unhandled promise rejection (rejection id: 1): Err

2019-07-23 16:47发布

I've made some research on the Web and SOF, but found nothing really helpful on that error.

I installed Node and Puppeteer with Windows 10 Ubuntu Bash, but didn't manage to make it work, yet I manage to make it work on Windows without Bash on an other machine.

My command is :

    node index.js

My index.js tries to take a screenshot of a page :

    const puppeteer = require('puppeteer');

    async function run() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    await page.goto('https://github.com');
    await page.screenshot({ path: 'screenshots/github.png' });

    browser.close();
    }

    run();

Does anybody know the way I could fix this "Error: kill ESRCH" error?

4条回答
一纸荒年 Trace。
2楼-- · 2019-07-23 17:05

I worked around it by softlinking chrome.exe to node_modules/puppeteer/.../chrome as below

ln -s /mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe  node_modules/puppeteer/.local-chromium/linux-515411/chrome-linux/chrome
查看更多
beautiful°
3楼-- · 2019-07-23 17:09

I had the same issue, this worked for me. Try updating your script to the following:

const puppeteer = require('puppeteer');

async function run() {
//const browser = await puppeteer.launch();
const browser = await puppeteer.launch({headless: true, args: ['--no-sandbox'] }); //WSL's chrome support is very new, and requires sandbox to be disabled in a lot of cases.
const page = await browser.newPage();

await page.goto('https://github.com');
await page.screenshot({ path: 'screenshots/github.png' });

await browser.close(); //As @Md. Abu Taher suggested
}

run();
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });

If you want to read all the details on this, this ticket has them (or links to them). https://github.com/Microsoft/WSL/issues/648

Other puppeteer users with similar issues: https://github.com/GoogleChrome/puppeteer/issues/290#issuecomment-322851507

查看更多
贼婆χ
4楼-- · 2019-07-23 17:18

I just fixed this issue. What you need to do is the following:

1) Install Debian dependencies

You can find them in this doc: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

sudo apt-get install all of those bad boys.

2) Add '--no-sandbox' flag when launching puppeteer

3) Make sure your windows 10 is up to date. I was missing an important update that allowed you to launch Chrome.

查看更多
做自己的国王
5楼-- · 2019-07-23 17:21

Points no consider:

  1. Windows bash is not a complete drop-in replacement for Ubuntu bash (yet). There are many cases where different GUI based apps did not work properly. Also, the script might be confused by bash on windows 10. It could think that the os is linux instead of windows.

  2. Windows 10 bash only supports 64-bit binaries, so make sure the node and the chrome version that's used inside is pretty much 64-bit. Puppeteer is using -child.pid to kill the child processes instead of child.pid on windows version. Make sure puppeteer is not getting confused by all these bash/windows thing.

Back to your case.

You are using browser.close() in the function, but it should be await browser.close(), otherwise it's not executing in proper order.

Also, You should try to add await page.close(); before browser.close();.

So the code should be,

await page.close();
await browser.close();
查看更多
登录 后发表回答