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:
/opt
. /opt/nodejs/node_modules/puppeteer
does exist.NODE_PATH
correctly includes /opt/nodejs/node_modules
You have to install your module with save
flag before uploading your zip to Amazon :
npm i puppeteer --save
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With