简体   繁体   中英

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

在将 zip 上传到 Amazon 之前,您必须安装带有save标志的模块:

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 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
npm i puppeteer --save

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM