13

In a AWS Lambda/NodeJS runtime, I'm attempting to import from an absolute path (/opt/nodejs/node_modules/puppeteer).

Source runs fine locally but, once bundled with Webpack/serverless-webpack and run in AWS Lambda, require('puppeteer') results in:

{"errorMessage":"Cannot find module 'puppeteer'","errorType":"Error","stackTrace":["webpackMissingModule (/var/task/src/render/handler.js:643:89)","/var/task/src/render/handler.js:643:173","next (native)","step (/var/task/src/render/handler.js:608:191)","/var/task/src/render/handler.js:608:361"]}

I've checked:

  • AWS Lambda Layer is mounted at /opt.
  • The path /opt/nodejs/node_modules/puppeteer does exist.
  • NODE_PATH correctly includes /opt/nodejs/node_modules
logicalicy
  • 857
  • 1
  • 9
  • 19
  • 1
    It would actually be more appropriate to include `puppeteer-core` and use `puppeteer` as a dev-dependency, then pass a executable path based off your environment to `browser.launch()`. Puppeteer-core is without chromium bundled, so you're safe to import and use it. The base puppeteer package will include chromium for local development, but won't get bundled due to being a dev dependency. – Dan Feb 21 '21 at 17:32

5 Answers5

21

You have to install your module with save flag before uploading your zip to Amazon :

npm i puppeteer --save
14

npm i --save puppeteer results in a too big package. (Max 50MB for Lambdas.)

So, instead, puppeteer was installed with npm i --save-dev puppeteer --ignore-scripts. (Ignore scripts to prevent Chromium from being installed.) The serverless-webpack plugin had to be told to ignore puppeteer in its packaging. (Otherwise puppeteer would bloat the package.)

The puppeteer module was put in a Layer (in the folder structure mentioned in the question) and require('puppeteer') now works.

logicalicy
  • 857
  • 1
  • 9
  • 19
1

Try running your script by forcing the environment variable $NODE_PATH. Such as:

NODE_PATH=/opt/nodejs/node_modules /path/to/bin/node your-file.js

For a specific reason I had to build from source a version of node without affecting the currently installation and this workaround worked for me.

I've got to this solution based on the following question here.

Iago F.
  • 21
  • 3
0

If you are using stencil.js it gives a very similar error. Try updating the stencil core version to at least "@stencil/core": "^1.15.0"

In your shell prompt try

npm install -g npm-check-updates
ncu -u
GiorgosK
  • 7,647
  • 2
  • 29
  • 26
0
npm i puppeteer --save

You have to install your module with save flag before uploading your zip

Tyler2P
  • 2,324
  • 26
  • 22
  • 31