简体   繁体   English

AWS Lambda 错误“找不到模块‘mongodb’”

[英]AWS Lambda error "Cannot find module 'mongodb'"

I've successfully deployed my function to AWS.我已成功将我的函数部署到 AWS。

When trying to invoke the function ( serverless invoke -f facebookAdInsert -l ), I get the following error:尝试调用该函数( serverless invoke -f facebookAdInsert -l )时,出现以下错误:

{
    "errorMessage": "Cannot find module 'mongodb'",
    "errorType": "Error",
    "stackTrace": [
        "Function.Module._load (module.js:474:25)",
        "Module.require (module.js:596:17)",
        "require (internal/module.js:11:18)",
        "Object.<anonymous> (/var/task/src/facebookAdInsert.js:222:18)",
        "__webpack_require__ (/var/task/src/facebookAdInsert.js:20:30)",
        "Object.<anonymous> (/var/task/src/facebookAdInsert.js:234:16)",
        "__webpack_require__ (/var/task/src/facebookAdInsert.js:20:30)",
        "Object.<anonymous> (/var/task/src/facebookAdInsert.js:303:16)",
        "__webpack_require__ (/var/task/src/facebookAdInsert.js:20:30)"
    ]
}

Here are my dependencies:这是我的依赖项:

"dependencies": {
    "@babel/core": "^7.3.4",
    "@babel/preset-env": "^7.3.4",
    "aws-sdk": "^2.268.1",
    "babel-loader": "^8.0.5",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "dotenv": "^6.0.0",
    "moment": "^2.24.0",
    "mongodb": "^3.1.10",
    "mongoose": "^5.4.17",
    "parse-mongo-url": "^1.1.1",
    "request": "^2.88.0",
    "serverless-offline": "^3.33.0",
    "serverless-webpack": "^5.2.0",
    "webpack": "^4.29.6",
    "webpack-node-externals": "^1.7.2"
}

Any ideas as to what the issue could be?关于问题可能是什么的任何想法? Based on my research possible solutions were running npm install before deploying and moving all "devDependencies" to "dependencies" but those have not worked.根据我的研究,可能的解决方案是在部署之前运行npm install并将所有“devDependencies”移动到“dependencies”,但这些都没有奏效。

You are missing the mongodb layer for AWS lambda您缺少 AWS lambda 的 mongodb 层

Open a terminal and type打开终端并输入

  1. mkdir nodejs mkdir nodejs
  2. cd nodejs cd nodejs
  3. npm init -y npm 初始化 -y
  4. npm install mongodb npm 安装 mongodb
  5. zip the entire nodejs folder, making sure that if you unzip the folder you get the following structure压缩整个 nodejs 文件夹,确保如果您解压缩文件夹,您将获得以下结构
    nodejs
      +---node_modules
             +---------- (mongodb and other folders)
  1. Upload this layer to AWS Lambda将此层上传到 AWS Lambda
  2. Add the layer to your Lambda function将层添加到您的 Lambda 函数

With this it should work without problems.有了这个,它应该可以正常工作。

Regards问候

I've seen this problem before when the application code was stored in a dotfolder (eg /.myapp/src ). 在将应用程序代码存储在dotfolder中(例如/.myapp/src )之前,我已经看到了此问题。 Can we see your serverless.yaml ? 我们可以看到您的serverless.yaml吗?

It looks like you've done the basic troubleshooting (making sure the dep is in package.json , and you're doing npm install ). 看来您已经完成了基本的疑难解答(确保dep在package.json ,并且您正在执行npm install )。

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

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