Playwright in functions - Failed to launch chromium because executable doesn't exist

Hi, I’m trying to use Playwright in a Netlify function but I get this error on deploy (works great locally with netlify dev):

browserType.launch: Failed to launch chromium because executable doesn't exist at /home/sbx_user1051/.cache/ms-playwright/chromium-854489/chrome-linux/chrome
Try re-installing playwright with "npm install playwright"Error
    at Object.captureStackTrace (/var/task/src/node_modules/playwright/lib/utils/stackTrace.js:48:19)
    at Connection.sendMessageToServer (/var/task/src/node_modules/playwright/lib/client/connection.js:69:48)
    at Proxy.<anonymous> (/var/task/src/node_modules/playwright/lib/client/channelOwner.js:64:61)
    at /var/task/src/node_modules/playwright/lib/client/browserType.js:64:67
    at BrowserType._wrapApiCall (/var/task/src/node_modules/playwright/lib/client/channelOwner.js:77:34)
    at BrowserType.launch (/var/task/src/node_modules/playwright/lib/client/browserType.js:55:21)
    at Runtime.exports.handler (/var/task/src/functions/points-balance-get.js:19:38)
    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)

I’ve try to empty cache, use npm instead of yarn but it does not work.

I tried to use the package playwright-aws-lambda but it weights 44MB and with other modules, it exceeded the 66MB limit.

I also read this thread but it did not help: [Feature] Support for AWS Lambda / Serverless environments · Issue #2404 · microsoft/playwright · GitHub

Thanks for your help.

So, how are you getting the binary in there, if the packages you’ve attempted are too large for lambdas? not being able to run it == it’s not there - so that’s the problem I think you’re trying to solve :slight_smile:

I’ve heard from some folks that the only way they could get a browser to fit into a lambda was to use puppeteer/headless chrome rather than a normal browser like chromium, in case that is an avenue you haven’t tried yet.

After additional investigations, I had the same conclusions as you. It’s not possible to user Playwright as is in a lambda.

However, I found this code sample that offers an alternative with Puppeteer and it works great:

const chromium = require('chrome-aws-lambda')
const puppeteer = require('puppeteer-core')

exports.handler = async (event) => {
  const executablePath = await chromium.executablePath
  // ...
}

Sadly, this code sample does not work locally with netlify dev command because the package is specifically designed for deployed lambda functions. And dynamic module imports based on environment is not a thing. This makes debugging very painful. :persevere:

hey there, thanks for updating us on this.

If you’d like, you could bring the last thing you mentioned here up to the netlify dev team by filing an issue here:

I was able to use this, thanks!
Here is how I got it to work in Netlify dev too:

// # install chromium with homebrew no quarantine (on mac osx, works with other OSes package installs too)
brew install chromium --no-quarantine

  if (process.env.NETLIFY_DEV) {
    var executablePath ='/opt/homebrew/bin/chromium';
  } else {
    var executablePath = await chromium.executablePath;
  }
  // setup
  browser = await puppeteer.launch({
    args: chromium.args,
    executablePath: executablePath,
    headless: chromium.headless,
  })

I found these workarounds here:

Cheers!