简体   繁体   English

找不到模块'puppeteer'

[英]Cannot find module 'puppeteer'

In a AWS Lambda/NodeJS runtime, I'm attempting to import from an absolute path ( /opt/nodejs/node_modules/puppeteer ).在 AWS Lambda/NodeJS 运行时中,我试图从绝对路径 ( /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:源代码在本地运行良好,但是一旦与 Webpack/serverless-webpack 捆绑在一起并在 AWS Lambda 中运行, require('puppeteer')会导致:

{"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 . AWS Lambda 层安装在/opt
  • The path /opt/nodejs/node_modules/puppeteer does exist.路径/opt/nodejs/node_modules/puppeteer确实存在。
  • NODE_PATH correctly includes /opt/nodejs/node_modules NODE_PATH正确包含/opt/nodejs/node_modules

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

npm i puppeteer --save

npm i --save puppeteer results in a too big package. npm i --save puppeteer导致包太大。 (Max 50MB for Lambdas.) (Lambda 最大 50MB。)

So, instead, puppeteer was installed with npm i --save-dev puppeteer --ignore-scripts .因此,puppeteer 与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. (忽略脚本以防止安装 Chromium。)必须告诉serverless-webpack插件忽略其包装中serverless-webpack (Otherwise puppeteer would bloat the package.) (否则 puppeteer 会使包膨胀。)

The puppeteer module was put in a Layer (in the folder structure mentioned in the question) and require('puppeteer') now works. puppeteer 模块被放置在一个层中(在问题中提到的文件夹结构中)并且require('puppeteer')现在可以工作了。

Try running your script by forcing the environment variable $NODE_PATH.尝试通过强制环境变量 $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.出于特定原因,我必须在不影响当前安装的情况下从源代码构建一个 node 版本,这个解决方法对我有用。

I've got to this solution based on the following question here .我已经根据以下问题here找到了这个解决方案。

If you are using stencil.js it gives a very similar error.如果您使用的是stencil.js,它会给出一个非常相似的错误。 Try updating the stencil core version to at least "@stencil/core": "^1.15.0"尝试将模板核心版本更新为至少"@stencil/core": "^1.15.0"

In your shell prompt try在你的 shell 提示符下试试

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在上传 zip 之前,您必须使用保存标志安装模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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