I’m working on a project that uses AWS Lambda with NodeJS and trying to load puppeteer from /opt/nodejs/node_modules/puppeteer. Everything works perfectly when I test it on my local machine, but when I deploy it to AWS Lambda using webpack and serverless-webpack, I get this error:
{"errorMessage":"Cannot find module 'puppeteer'","errorType":"Error","stackTrace":["webpackMissingModule (/var/task/src/app/main.js:512:45)","/var/task/src/app/main.js:512:128","next (native)","step (/var/task/src/app/main.js:487:156)","/var/task/src/app/main.js:487:298"]}
I have already verified these things:
- My Lambda Layer is properly mounted at
/opt
- The puppeteer module exists at
/opt/nodejs/node_modules/puppeteer
NODE_PATH environment variable includes /opt/nodejs/node_modules
The require('puppeteer') statement works locally but fails in the Lambda environment. Has anyone encountered this issue before? What could be causing webpack to not find the module even though it exists in the correct location?
Been there with Lambda layers and bundlers. Webpack’s usually trying to resolve modules at build time instead of runtime. I fixed it by changing the require statement to use an absolute path like require('/opt/nodejs/node_modules/puppeteer') instead of just require('puppeteer'). Forces Node to look exactly where your layer mounts the module. Also check if your webpack config has resolve aliases or module resolution settings that might mess things up. Sometimes bundlers get confused about where to find dependencies even when NODE_PATH is set right.
maybe your webpack setup is missing somethin. try adding externals: {'puppeteer': 'puppeteer'} in your webpack.config.js to make sure it’s not included in the bundle. that way it looks for it at runtime. hope this helps!
I had the same issue. Webpack was trying to bundle puppeteer instead of treating it as external. Check your serverless-webpack config - in serverless.yml, set includeModules: false under the webpack plugin. This stops webpack from auto-including modules that should come from your Lambda layer. Also verify your layer ARN is properly referenced in the function config. Sometimes the layer gets detached during deployment without any obvious error.