35

This Meteor code uses "puppeteer 8.0.0", "puppeteer-core 10.0.0", puppeteer-extra 3.1.18" and "puppeteer-extra-plugin-stealth 2.7.8", It gives this error:

Error: Could not find expected browser (chrome) locally. Run npm install to download the correct Chromium revision (884014).

Tried "npm install" for no avail. Reading up online, tried removing "puppeteer-core": "^10.0.0" from package.json dependencies for no avail.

Any help is much appriciated. Thanks

const puppeteer = require('puppeteer-extra');
const nameH = require('./NameH');

const puppeteerOptions = {
    headless: true,
    ignoreHTTPSErrors: true,
    args: ['--no-sandbox', '--single-process', '--no-zygote', '--disable-setuid-sandbox']
}

let browser;
let pageNameH;

const init = async () => {
    const StealthPlugin = require('puppeteer-extra-plugin-stealth');
    console.log('1')         //>>>>>>>>>>>> Prints 1
    puppeteer.use(StealthPlugin());
    console.log('2')         //>>>>>>>>>>>> Prints 2
    
    browser = await puppeteer.launch(puppeteerOptions);
    console.log('3') //>>>>>>>>> DID NOT PRINT <<<<<<<<<<<<<<<
    pageNameH = await browser.newPage();
    console.log('4')

    await pageNameH.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
    await pageNameH.setViewport({ width: 1366, height: 768 });
    await pageNameH.setRequestInterception(true);
    blockResources(pageNameH);
}

const blockResources = page => {
    page.on('request', (req) => {
    if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') {
        req.abort();
    }
    else {
        req.continue();
    }
    });
}


export const abc = async (nm, loc) => {
    try {
    console.log('name try')       //>>>>>>>>>>>> Prints "name try"
    if (!(browser && pageNameH))
        await init();
    //use "required" nameh here

    } catch (error) { // print the error <<<<<<<<<<<<<<<<<<<<<<<<<
    console.log("Could not launch Puppeteer or open a new page.\n" + error);
    if (browser && browser.close === 'function') await browser.close();
    }
}


// included in package.json
"dependencies": {
    "@babel/runtime": "^7.11.2",
    "axios": "^0.21.1",
    "check": "^1.0.0",
    "cheerio": "^1.0.0-rc.6",
    "jquery": "^3.5.1",
    "meteor-node-stubs": "^1.0.1",
    "nightmare": "^3.0.2",
    "pending-xhr-puppeteer": "^2.3.3",
    "puppeteer": "^8.0.0",
    "puppeteer-core": "^10.0.0",
    "puppeteer-extra": "^3.1.18",
    "puppeteer-extra-plugin-adblocker": "^2.11.11",
    "puppeteer-extra-plugin-block-resources": "^2.2.9",
    "puppeteer-extra-plugin-stealth": "^2.7.8"
},
Sebastian
  • 1,321
  • 9
  • 21
Fred J.
  • 5,759
  • 10
  • 57
  • 106

12 Answers12

86

may be you can try this, it works for me on linux(centOS), puppeteer(10.2.0).

cd ./node_modules/puppeteer
npm run install

If that fails, you can also try running:

cd ./node_modules/puppeteer
npm install

This will download the chromium to ./node_modules/puppeteer/.local-chromium

i Mr Oli i
  • 605
  • 5
  • 12
Jin Liu
  • 869
  • 5
  • 4
12

Fixed it by running this command to install chromium manually.

node node_modules/puppeteer/install.js
Himanshi Gupta
  • 131
  • 1
  • 2
9

I had the same problem. I checked my env variables, and even though PUPPETEER_SKIP_CHROMIUM_DOWNLOAD was set to false, it was still not working. After I removed the variable (unset PUPPETEER_SKIP_CHROMIUM_DOWNLOAD for mac), it worked.

related dependancies:

  "dependencies": {
    "chrome-aws-lambda": "^10.0.0",
    "puppeteer-core": "^10.0.0",
  },
  "devDependencies": {
    "puppeteer": "^10.0.0",
  }

Launching Chromium:

    import chromium from "chrome-aws-lambda";

    const browser = await chromium.puppeteer.launch({
        executablePath: await chromium.executablePath,
    });
Alx
  • 101
  • 3
  • Why is "puppeteer-core" a dependency even though you don't use it in the code? Specifically `chromium.puppeteer.launch()` instead of `puppeteer.launch()` – Nermin May 03 '23 at 18:06
  • I tried these exact dependency versions and got but got `WARNING: crash_report_exception_handler.cc(240)] UniversalExceptionRaise: (os/kern) failure (5)` – Nermin May 03 '23 at 18:18
6

if you are testing locally, make sure you have puppeteer installed as dev dependencies. specifically

npm install puppeteer --save-dev

https://github.com/alixaxel/chrome-aws-lambda/wiki/HOWTO:-Local-Development#workaround

this approach allows us to rely on puppeteer for local development and puppeteer-core for production deployments.

fung933
  • 61
  • 3
  • I copy pasted the npm install instruction in that README but got an error: `Could not resolve dependency: npm ERR! peer puppeteer-core@"^10.1.0" from chrome-aws-lambda@10.1.0` – Nermin May 03 '23 at 18:16
4

I had the same issue. What worked for me was to specify as the executablePath Puppeteer launch option the fullpath to the local chromium used by Puppeteer.

Something like this:

const launchOptions = {
  // other options (headless, args, etc)
  executablePath: '/home/jack/repos/my-repo/node_modules/puppeteer/.local-chromium/linux-901912/chrome-linux/chrome'
}

As noted in another answer, it seems that also referencing a chromium local binary would work, but I think it's a worse solution, since Puppeteer is guaranteed to work only with the local bundled version of Chromium.

jackdbd
  • 4,583
  • 3
  • 26
  • 36
  • This answer is so underrated. In my case, I don't want full puppeteer. Just puppeteer-core is ok, but it just couldn't find the browser on my ubuntu 18. I just needed to tell it where it is then it worked. Thank you. – hbobenicio Nov 17 '21 at 17:37
3

Throwing my answer in, in hopes that it helps someone not waste their entire evening like I did.

I was writing a Typescript server that used Puppeteer, and I'm using ESBuild to transpile from TS to JS. In the build step, esbuild was trying to bundle everything into one file, but I had to instruct it to preserve the Puppeteer import from node_modules.

I did this by marking puppeteer as external. See docs here.

Since npm i downloads a compatible version of chromium to the node_modules folder, once I preserved this import, it was able to find Chromium in the node_modules folder.

My build file looks like:

require("esbuild").buildSync({
  entryPoints: ["src/index.ts"],
  outdir: "build",
  bundle: true,
  platform: "node",
  target: "node16",
  external: ["puppeteer"],
});

And I run it with node prod-build.js.

Now in my code, I can just call launch!
const browser = await puppeteer.launch()

TJBlackman
  • 1,895
  • 3
  • 20
  • 46
  • 1
    Thanks for your answer. i'm testing rather a not popular setup - Angular Universal where Express serves both for REST API and for SSR. Adding the "puppeteer" to _externals_ of a custom webpack config for server app solved the problem! – Misha Borisov Jan 15 '22 at 22:28
  • This approach pointed me in the right direction. There's another question (and answer) that highlights the exact steps when using angular univeral. This solved the issue for me, so I'm also linking it here: https://stackoverflow.com/questions/70622351/the-angular-universal-ssr-cannot-integrate-with-the-external-libraries-like-pu – Grochni Nov 24 '22 at 16:02
0

I used the installed version on my pc (maybe not what you're looking for)

const browser = await puppeteer.launch({headless:false,  executablePath: 
           'C:/Program Files/.../chrome.exe' });
Petronella
  • 2,327
  • 1
  • 15
  • 24
0

You may need to install some dependencies depending on your OS. Check Puppeteer's Troubleshooting page for more details.

Giovanni Benussi
  • 3,102
  • 2
  • 28
  • 30
0

I was having this error too and I noticed that I started getting such an error after updating my node from my Dockerfile to version ^16 (my puppeteer is in a container). How did I solve it? I downgraded my node from Dockerfile to version 12. Hope this resolves it...

OBS: I use Ubuntu 21.04 in machine.

EDIT ------

In newest versions of node, you need to go in ./node_modules/puppeteer and run command npm install, then the correct packages will be installed. I'm using that solution.

pedr0vvs
  • 1
  • 1
0

If you are using puppeteer in AWS SAM and you don't have puppeteer in dependencies, you can install the same in puppeteer-core using

   node node_modules/puppeteer/install.js

For this setup to work you will have to add chrome-aws-lambda in the devDependencies.

    "devDependencies": {
        "chrome-aws-lambda": "^10.1.0"
     }

Also before you take it to production don't forget to add the layer in your template.yaml file:

Layers:
     - !Sub 'arn:aws:lambda:ap-south-1:764866452798:layer:chrome-aws-lambda:25'
user11666461
  • 761
  • 7
  • 12
0

For me the issue was that I ran npm install although I shouldn't have. Just deleted the node_modules folder and then ran again - now worked

Chef Lax
  • 89
  • 2
  • 10
-1

Delete the node_modules folder and run

npm install
Tyler2P
  • 2,324
  • 26
  • 22
  • 31