Module 'puppeteer' not found in AWS Lambda/NodeJS environment

I’m having trouble with my AWS Lambda function using NodeJS. The code works fine on my local machine, but when I deploy it using Webpack and serverless-webpack, I get an error saying it can’t find the ‘puppeteer’ module.

Here’s what I’ve done:

  • Set up an AWS Lambda Layer mounted at /opt
  • Confirmed the puppeteer module is in /opt/nodejs/node_modules/puppeteer
  • Added /opt/nodejs/node_modules to the NODE_PATH

But when I try to require('puppeteer'), I still get this error:

{
  "errorMessage": "Cannot find module 'puppeteer'",
  "errorType": "Error",
  "stackTrace": [
    "webpackMissingModule (/var/task/src/process/main.js:512:67)",
    "/var/task/src/process/main.js:512:145",
    "next (native)",
    "step (/var/task/src/process/main.js:487:159)",
    "/var/task/src/process/main.js:487:293"
  ]
}

Any ideas on what I’m missing or how to fix this? I’m stumped!

hav u checked ur webpack config? it might be excluding external modules. try adding puppeteer to externals in ur webpack.config.js:

externals: ['puppeteer']

this tells webpack not to bundle puppeteer and use the one in lambda layer instead. hope it helps!

I’ve dealt with this exact issue before, and it can be a real headache. One thing that worked for me was explicitly requiring the Puppeteer module from the Lambda Layer path. Try modifying your code to something like this:

const puppeteer = require(‘/opt/nodejs/node_modules/puppeteer’);

This bypasses Node’s module resolution and directly points to where Puppeteer should be. Also, make sure your Lambda function’s timeout is set high enough. Puppeteer can take a while to initialize, especially on cold starts.

If that doesn’t work, you might want to consider using chrome-aws-lambda instead. It’s specifically designed for use with AWS Lambda and includes a bundled version of Chromium, which can simplify your setup considerably.

Lastly, double-check your Lambda function’s memory allocation. Puppeteer can be pretty resource-intensive, so you might need to bump it up if you’re running into issues.

I encountered a similar issue when deploying a Lambda function with Puppeteer. The problem might be related to how serverless-webpack handles dependencies. Have you tried using the ‘serverless-plugin-include-dependencies’ plugin? It can help ensure all necessary modules are included in your deployment package.

Also, double-check your Lambda function’s execution role. It needs permissions to access the Layer you’ve created. Sometimes, permission issues can masquerade as module not found errors.

Lastly, consider logging the contents of /opt/nodejs/node_modules at the start of your Lambda function execution. This can help verify if the Puppeteer module is actually present where you expect it to be during runtime.