chrome-aws-lambda: [BUG] Cannot find module 'puppeteer-core/lib/Browser'

Trying to execute README example, the following error occurred.

Environment

  • chrome-aws-lambda Version: 3.1.1
  • puppeteer / puppeteer-core Version: 5.1.0
  • OS: Linux
  • Node.js Version: 12.x
  • Lambda / GCF Runtime: nodejs12.x

Expected Behavior

Should have returned example.com content.

Current Behavior

Following error occurs:

Lambda execution failed with status 200 due to customer function error:  Cannot find module 'puppeteer-core/lib/Browser'
Require stack:
- /var/task/node_modules/chrome-aws-lambda/source/puppeteer/lib/Browser.js
- /var/task/node_modules/chrome-aws-lambda/source/index.js
- /var/task/app.js

No Browser module expored in the following path: ‘puppeteer-core/lib/Browser’

Steps to Reproduce

Lambda functions: memory is 1024M, timeout is 3000ms.

const chromium = require('chrome-aws-lambda');

exports.handler = async (event, context, callback) => {
  let result = null;
  let browser = null;

  try {
    browser = await chromium.puppeteer.launch({
      args: chromium.args,
      defaultViewport: chromium.defaultViewport,
      executablePath: await chromium.executablePath,
      headless: chromium.headless,
      ignoreHTTPSErrors: true,
    });

    let page = await browser.newPage();

    await page.goto(event.url || 'https://example.com');

    result = await page.title();
  } catch (error) {
    return callback(error);
  } finally {
    if (browser !== null) {
      await browser.close();
    }
  }

  return callback(null, result);
};

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 21
  • Comments: 20

Most upvoted comments

i had to move away from chrome-aws-lambda completely but did find a solution by following this: https://www.npmjs.com/package/@sparticuz/chromium.

as the docs advise, i created a layer for a specific chromium version that shipped with the version of puppeteer-core i was using, added it to my function, and successfully ran a (very) simple script.

basic idea:

  • reference the chromium support page for proper version mapping
  • create archive of the proper chromium version for a layer, publish zip to s3, create new version
  • add layer to function
  • use chromium args with puppeteer-core

here’s the setup i got working:

// lambda fn
runtime: Node.js 16.x
architecture: x86_64
memory: 512MB
// index.js
const puppeteer = require("puppeteer-core");
const chromium = require("@sparticuz/chromium");

exports.handler = async (event) => {

  const browser = await puppeteer.launch({
    args: chromium.args,
    defaultViewport: chromium.defaultViewport,
    executablePath: await chromium.executablePath(),
    headless: chromium.headless,
    ignoreHTTPSErrors: true,
  });

  const page = await browser.newPage();

  await page.goto("https://example.com");

  const title = await page.title();
  const content = await page.content();
  
  await browser.close();
  
  return {
    title,
    content,
  }
}
// invocation results
Duration: 8712.36 ms Billed Duration: 8713 ms Memory Size: 512 MB Max Memory Used: 489 MB Init Duration: 371.51 ms

using v16 was a leftover config from trying to mix and match older runtimes with various versions of chrome-aws-lambda and puppeteer-core. have yet to try with v18 but intend to do so and update this thread.

13.7.0 works fine for me

Also downgrading to versions 3.0.4 of chrome-aws-lambda and puppeteer-core works for me

@benjaminDanis does it work on Node v18?

As a workaround I am using the regular puppeteer at 5.2, the paths for that were already fixed in: https://github.com/alixaxel/chrome-aws-lambda/commit/f4d1fed53f0a20782900483b7c11539ba13faad9

To keep my lambda size down I run rm -r node_modules/puppeteer/.local-chromium prior to uploading.

This has as an additional benefit that pptr-testing-library/extend works as well.

Same error, and solved.

“puppeteer-core” seems to have changed the location of Browser.tsx from version 5. So I use version 4.0.1.

yarn remove puppeteer-core

and

yarn add puppeteer-core@4.0.1

I use this until I next update.

@benjaminDanis kinda late but v18 works?

@JadeMin nodev18 does work, just confirmed.

I can confirm this works with nodev20 as well. That is the solution @benjaminDanis proposed