Puppeteer runtime error >> Could not find Chromium (rev. 1108766)

This is a very recent error I encountered when running puppeteer on a deployed draft site. I don’t claim this is a Netlify issue, but maybe I can get pointed in the right direction.

Full error text is below:

ERROR! Error: Could not find Chromium (rev. 1108766). This can occur if either 1. you did not perform an installation before running the script (e.g. npm install) or 2. your cache path is incorrectly configured (which is: /home/sbx_user1051/.cache/puppeteer). For (2), check out our guide on configuring puppeteer at Configuration | Puppeteer.

and the draft site is:

https://642f3487fd33ef0a93ae06c3--pgnfen3.netlify.app/; go to Import PGN Files (this calls a lambda function, which uses puppeteer to access a web site)

Things I’ve tried:

  1. set AWS_LAMBDA_JS_RUNTIME to nodejs18.x
  2. disabled cache in puppeteer: await page.setCacheEnabled(false)
  3. cleared the browser cache
  4. cleared the build cache: yarn cache clear --force
  5. deployed with --skip-functions-cache

Did the cache path become invalid? It used to work with a simple netlify deploy

Here’s the build log:

 Netlify Build                                                 
────────────────────────────────────────────────────────────────
​
❯ Version
  @netlify/build 29.5.8
​
❯ Flags
  dry: false
  offline: false
​
❯ Current directory
  /home/jlowery2663/pgnfen3
​
❯ Config file
  /home/jlowery2663/pgnfen3/netlify.toml
​
❯ Context
  production
​
  1. build.command from netlify.toml                            
────────────────────────────────────────────────────────────────
​
$ react-scripts build
Creating an optimized production build...
Compiled successfully.

File sizes after gzip:

  605.7 kB  build/static/js/main.7a5a1832.js
  2.55 kB   build/static/css/main.56d686b3.css
  1.78 kB   build/static/js/787.ffec13a5.chunk.js

The bundle size is significantly larger than recommended.
Consider reducing it with code splitting: https://goo.gl/9VhYWB
You can also analyze the project dependencies: https://goo.gl/LeUzfb

The project was built assuming it is hosted at /.
You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.
You may serve it with a static server:

  yarn global add serve
  serve -s build

Find out more about deployment here:

  https://cra.link/deployment

​
(build.command completed in 19.8s)
​
  2. Functions bundling                                         
────────────────────────────────────────────────────────────────
​
Packaging Functions from netlify/functions directory:
 - testgql.js
 - pgnfen/pgnfen.js
​
​
❯ The following Node.js modules use dynamic expressions to include files:
   - yargs
   - yargs-parser
​
  Because files included with dynamic expressions aren't bundled with your serverless functions by default,
  this may result in an error when invoking a function. To resolve this error, you can mark these Node.js
  modules as external in the [functions] section of your `netlify.toml` configuration file:
​
  [functions]
    external_node_modules = ["yargs", "yargs-parser"]
​
  Visit https://ntl.fyi/dynamic-imports for more information.
​
​
(Functions bundling completed in 7.4s)
​
  Netlify Build Complete                                        
────────────────────────────────────────────────────────────────

Dumping __dirname and cwd in the function log, I see:

  __dirname: '/var/task/netlify/functions/pgnfen',
  cwd: '/var/task'

I’m about to try setting the cache directory off of …/functions/pgnfen or /var/task

Set PUPPETEER_CACH_DIR env var to /var/task/netlify/functions/pgnfen/.cache/puppeteer

Added the cache directory to my functions folder, then ran build/deploy --prod

Getting similar error as before, but it does show the ‘correct’ cache dir:

ERROR! Error: Could not find Chromium (rev. 1108766). This can occur if either 1. you did not perform an installation before running the script (e.g. npm install) or 2. your cache path is incorrectly configured (which is: /var/task/netlify/functions/pgnfen/.cache/puppeteer). For (2), check out our guide on configuring puppeteer at Configuration | Puppeteer.

The function log file shows my console dump of __dirname, process.cwd(), and process.env.PUPPETEER_CACHE_DIR:

  __dirname: '/var/task/netlify/functions/pgnfen',
  cwd: '/var/task',
  cacheDir: '/var/task/netlify/functions/pgnfen/.cache/puppeteer'

At this point I’m at a loss as to how to proceed. As I stated in the OP, this was recently working with a simple netlify build and netlify deploy

This may have something to do with it:

[puppeteer/troubleshooting.md at main · puppeteer/puppeteer · GitHub]

Does forcing the Chrome binary to be included work:

There are lambda versions of chrome out there (somewhat dated) that can be installed via package.json and imports. Not ideal, but I’ll probably do that.

There is one other option, and that is for Netlify to install Chromium in a well-known location that can be accessed by all functions. Details here.

I understand that decision would be based on the demand for it. I know I ran across at least one article about using puppeteer in Netlify lambda functions, so I’m not alone.

I never got past the errors trying to run puppeteer or playwright in a lambda function.

Originally I had used node https.request(), but there was some origin header field a site was rejecting that I wasn’t able to override (and can’t remember which now, but I know that browsers don’t have to pass it for GET requests, surprisingly enough).

Thankfully, that original authentication problem is no more, apparently by magic, and I am able to avoid all that headless browser mess. So I am ‘solutioning’ this response.

That’s awesome! Thank for sharing this with the community @JeffML