繁体   English   中英

AWS Lambda 函数错误:找不到模块“lambda”

[英]AWS Lambda function error: Cannot find module 'lambda'

我正在尝试使用无服务器在 AWS 中部署 REST API。 节点版本 14.17.5。

我的目录结构:

在此处输入图片说明

当我成功部署上述内容时,在尝试访问 api 时出现以下错误。

2021-09-28T18:32:27.576Z    undefined   ERROR   Uncaught Exception  {
    "errorType": "Error",
    "errorMessage": "Must use import to load ES Module: /var/task/lambda.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/lambda.js from /var/runtime/UserFunction.js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.\nInstead rename lambda.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.\n",
    "code": "ERR_REQUIRE_ESM",
    "stack": [
        "Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/lambda.js",
        "require() of ES modules is not supported.",
        "require() of /var/task/lambda.js from /var/runtime/UserFunction.js is an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which defines all .js files in that package scope as ES modules.",
        "Instead rename lambda.js to end in .cjs, change the requiring code to use import(), or remove \"type\": \"module\" from /var/task/package.json.",
        "",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)",
        "    at Module.load (internal/modules/cjs/loader.js:937:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
        "    at Module.require (internal/modules/cjs/loader.js:961:19)",
        "    at require (internal/modules/cjs/helpers.js:92:18)",
        "    at _tryRequire (/var/runtime/UserFunction.js:75:12)",
        "    at _loadUserApp (/var/runtime/UserFunction.js:95:12)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:1072:14)"
    ]
}

根据错误中的建议,我尝试将 lambda.js 更改为 lambda.cjs。 现在我收到以下错误

 2021-09-28T17:32:36.970Z   undefined   ERROR   Uncaught Exception  {
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'lambda'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"stack": [
    "Runtime.ImportModuleError: Error: Cannot find module 'lambda'",
    "Require stack:",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1072:14)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)",
    "    at Module.load (internal/modules/cjs/loader.js:937:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
    "    at internal/main/run_main_module.js:17:47"
    ]
}

无服务器.yml

service: APINAME   #Name of your App
useDotenv: true
configValidationMode: error

provider:
 name: aws
 runtime: nodejs14.x # Node JS version
 memorySize: 512
 timeout: 15
 stage: dev
 region: us-east-1 # AWS region
 lambdaHashingVersion: 20201221

functions:
 api:
   handler: lambda.handler
   events:
     - http: ANY /{proxy+}
     - http: ANY /

拉姆达.js

import awsServerlessExpress from 'aws-serverless-express'
import app from './index.js'

const server = awsServerlessExpress.createServer(app)
export const handler = (event, context) => {
    awsServerlessExpress.proxy(server, event, context)
}

aws-cli 命令

 docker run --rm -it amazon/aws-cli --version
 docker run --rm -it amazon/aws-cli configure
 docker run --rm -it amazon/aws-cli serverless deploy

无服务器命令:

 docker run --rm -it amazon/aws-cli serverless deploy
 serverless config credentials --provider aws --key <KEY> --secret <SECRET>
 node ./node_modules/serverless/bin/serverless config credentials --provider aws --key <KEY> --secret <SECRET>

在阅读了几个答案后,我尝试了以下方法:

  1. 确保 package.json 包含 "type": "module"
  2. 删除了 node_modules 和 package-lock.json 并重新安装了它们(因为在开发过程中更新了 node 版本)

我究竟做错了什么?

将所有导入转换为 require() 并将所有导出转换为 module.exports

从 package.json 中删除了 "type": "module"

一切都像一个魅力。 这不是所问问题的解决方案,而是让事情发挥作用变得更加重要。

而是将 lambda.js 重命名为以 .cjs 结尾,更改需要使用的代码

import(),

remove \"type\": \"module\" from /var/task/package.json."

它在错误中。

暂无
暂无

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

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