11

When I try to run node app.js, I get error:

the message is Failed to launch the browser process! spawn /Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app

EACCES

What I did I checked the folder at /Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app and the file is not zipped. It can be run.

Note: If I try to execute without the path, it works, but I would like to use either Chrome or Chromium to open a new page. const browser = await puppeteer.launch({headless:false'});

const express = require('express');
const puppeteer = require('puppeteer');
const app = express();

(async () => {
  const browser = await puppeteer.launch({headless:false, executablePath:'/Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app'});
 const page = await browser.newPage();
 await page.goto('https://google.com', {waitUntil: 'networkidle2'});

})().catch((error) =>{
  console.error("the message is " + error.message);
});

app.listen(3000, function (){
    console.log('server started');
})

enter image description here

bibscy
  • 2,598
  • 4
  • 34
  • 82
  • if you navigate to `chrome://version/` page on this exact chromium, what is the value of the Executable Path? usually it looks like this on MAC: `/Applications/Google Chrome.app/Contents/MacOS/Google Chrome` (of course it can differ in case of bundled chromium in node_modules). _note:_ the chromium bundled with your puppeteer version is the right version that matches the api on pptr's side: if you plan to use other chrome/or chromium based browser you might experience unexpected issues. – theDavidBarton Sep 29 '20 at 11:36
  • 1
    The Chromium path is ```Executable Path /Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app/Contents/MacOS/Chromium```. My understanding is that I could use either Chrome, Chromium or Mozilla as long as I assign the correct path to ```puppeteer.launch({ executablePath: });``` and that browser does not have to necessarily be located in the ````/myProject/node_modules/```. – bibscy Sep 30 '20 at 09:34
  • 1
    correct: you can add any chromium based browser executables, but not all of them are garanteed to work together with puppeteer (as they can contain not compatible api), only the bundled chromium is garateed to work perfectly (the one that launches if you don't set custom executablePath). in case of firefox, you need to apply product related env variable as well, but that's another story. now if you compare the string you used for `executablePath` it differs from the one you've just retrieved. exactly the `/Contents/MacOS/Chromium` should be added to the end of the path to make it work. – theDavidBarton Sep 30 '20 at 09:41
  • Please put your comments in an answer so that I can vote it as the correct Answer. Many thanks. – bibscy Oct 02 '20 at 10:01

3 Answers3

23

If you navigate to chrome://version/ page in this exact browser, it will show the Executable Path which is the exact string you need to use as executablePath puppeteer launch option.

Usually, chrome's path looks like this on MAC:

/Applications/Google Chrome.app/Contents/MacOS/Google Chrome

Or something like this if chromium is located in your node_modules folder:

/Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app/Contents/MacOS/Chromium

Now if you compare the string you used for executablePath: it differs from the one retrieved with the method mentioned above. Exactly the /Contents/MacOS/Chromium should be added to the end of the current path to make it work.

Note: the chromium bundled with puppeteer is the version guaranteed to work together with the actual pptr version: if you plan to use other chrome/or chromium-based browsers you might experience unexpected issues.

theDavidBarton
  • 7,643
  • 4
  • 24
  • 51
9

Following up on @theDavidBarton:

Chromium which was shipped with Puppeteer did not work, but the Chrome installation on my MacBook did work.

OS: OS-X 10.15.7 (Catalina) Node version: v14.5.0

Failed code:

const browser = await puppeteer.launch({
  headless: true, 
  executablePath: "/users/bert/Project/NodeJS/PuppeteerTest/node_modules/puppeteer/.local-chromium/mac-818858/chrome-mac/Chromium.app/Contents/MacOS/Chromium"
});

Successful code:

const browser = await puppeteer.launch({
  headless: true, 
  executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
});

Full code, Just the first example on the Puppeteer website:

const puppeteer = require('puppeteer');

(async () => {
    try {
        const browser = await puppeteer.launch({headless: true, executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"});
        const page = await browser.newPage();
        await page.goto('https://example.com');
        await page.screenshot({path: 'example.png'});

        await browser.close();
    } catch (err) {
        console.log(err);
    }
})();

And, yes, I got the Screenshot !! :-)

BertC
  • 2,243
  • 26
  • 33
6

Using location-chrome: https://www.npmjs.com/package/locate-chrome

const locateChrome = require('locate-chrome');
const executablePath = await new Promise(resolve => locateChrome(arg => resolve(arg)));
const browser = await puppeteer.launch({ executablePath });
Albanir Neves
  • 183
  • 2
  • 10