Reputation: 953
I've tried with Puppeteer v5.4.0, v5.4.1, and v5.5.0, Windows 10/MacOS, and Node v12/v14.0.1/v15.0.3 to try to get Puppeteer to run in Electron.
Doing a simple puppeteer.launch()
like this in the main process of a React application:
const puppeteer = require('puppeteer');
(async function(){
const browser = await puppeteer.launch();
})();
returns this:
Uncaught (in promise) Error: Could not find browser revision 818858. Run "PUPPETEER_PRODUCT=firefox npm install" or "PUPPETEER_PRODUCT=firefox yarn install" to download a supported Firefox browser binary.
at ChromeLauncher.launch (Launcher.js:120)
at async http:/localhost:8080/main.js:61295
I've tried running PUPPETEER_PRODUCT=firefox yarn install
, setting the executablePath
in the launch()
method, deleting node_modules
and package-lock.json
, but none of these solutions seem to work. In the case of setting a path, it returns a ENOENT
error. Weird enough, though, writing a small script and running it through node test.js
seems to work fine.
Upvotes: 2
Views: 3237
Reputation: 953
After some hours of tinkering, here's how I figured it out:
Instead of calling from the main process, set the function in the renderer process and call it, like this:
/* main.js - Renderer Process */
ipcMain.on('spawn-browser', (event, arg) => {
const browser = await puppeteer.launch();
// Your other functions...
});
And from the main process, you can call something like:
require('electron').ipcRenderer.send('spawn-browser', args);
which works.
Upvotes: 3
Reputation: 6346
According to this, try one of the following:
sudo npm install puppeteer --unsafe-perm=true --allow-root
Or:
for linux:
1- you must have installed chromium browser using this command :
$sudo apt install -y chromium-browser
2- you have to get the excutable path of chromium using this command :
$which chromium-browser
3-put the executable path as an argument to the launch function :
const puppeteer = require('puppeteer-core');
(async () => {
const browser = await puppeteer.launch({
executablePath: '/usr/bin/chromium-browser',
headless: false
});
const page = await browser.newPage();
await page.goto('https://google.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
Upvotes: 2