Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module 'puppeteer'

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
like image 814
logicalicy Avatar asked Dec 13 '18 15:12

logicalicy


3 Answers

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

npm i puppeteer --save
like image 98
Abderrahim Soubai-Elidrisi Avatar answered Oct 19 '22 17:10

Abderrahim Soubai-Elidrisi


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.

like image 21
logicalicy Avatar answered Oct 19 '22 17:10

logicalicy


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.

like image 1
Iago F. Avatar answered Oct 19 '22 19:10

Iago F.