我在 AWS lambda 中收到引用错误
2022-11-19
3539
我正在尝试学习制作一个 Web 应用程序,并尝试遵循 AWS 提供的教程,但在制作 Lambda 函数时遇到了这个问题。
{
"errorType": "ReferenceError",
"errorMessage": "exports is not defined in ES module scope",
"trace": [
"ReferenceError: exports is not defined in ES module scope",
" at file:///var/task/index.mjs:3:1",
" at ModuleJob.run (node:internal/modules/esm/module_job:193:25)",
" at async Promise.all (index 0)",
" at async ESMLoader.import (node:internal/modules/esm/loader:530:24)",
" at async _tryAwaitImport (file:///var/runtime/index.mjs:921:16)",
" at async _tryRequire (file:///var/runtime/index.mjs:970:86)",
" at async _loadUserApp (file:///var/runtime/index.mjs:994:16)",
" at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1035:21)",
" at async start (file:///var/runtime/index.mjs:1200:23)",
" at async file:///var/runtime/index.mjs:1206:1"
]
}
index.mjs
// Define handler function, the entry point to our code for the Lambda service
// We receive the object that triggers the function as a parameter
exports.handler = async (event) => {
// Extract values from event and format as strings
let name = JSON.stringify(`Hello from Lambda, ${event.firstName} ${event.lastName}`);
// Create a JSON object with our response and store it in a constant
const response = {
statusCode: 200,
body: name
};
// Return the response constant
return response;
};
JSON { "firstName":"Tyler", "lastName":"Schnitzer"
它应该是一个简单的 hello world,但我很困惑为什么我无法让事件正常工作?
我希望有人能帮助解释这个错误以及如何解决它,我尝试查看 AWS Lambda 故障排除页面,但我仍然不明白。
2个回答
通过更改解决了同样的问题:
exports.handler = (event, context, callback) => {
为
export const handler = (event, context, callback) => {
在此 AWS 论坛 中找到了此解决方案。
inwx
2023-10-24
我遇到了同样的错误。我删除了 lambda 函数,并使用 Node.js 14.x 运行时重新创建了该函数,它运行正常。错误出在 Node.js 18.x 上,这是撰写本文时的默认版本。 我按照这个教程操作, https://www.eventbox.dev/published/lesson/innovator-island/2-realtime/2-backend.html
xahiru
2022-11-23