Unhandled promise rejection (rejection id: 1): Error: kill ESRCH

倖福魔咒の 提交于 2019-12-11 01:29:34

问题


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?


回答1:


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




回答2:


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.




回答3:


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();



回答4:


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


来源:https://stackoverflow.com/questions/47266005/unhandled-promise-rejection-rejection-id-1-error-kill-esrch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!