[英]AWS Lambda "cannot find module aws-sdk" in Build a Basic Web Application tutorial
I'm trying to get a start with AWS by starting at the beginning, running the Build a Basic Web Application tutorial.我试图通过从头开始运行Build a Basic Web Application教程来开始使用 AWS。 I figured I'd be able to just follow it step by step and have a simple Hello World web page front end to a little database.
我想我可以一步一步地跟着它做,并有一个简单的 Hello World 网页前端到一个小数据库。 But I'm running into a strange error: including the bare-bones, default
aws-sdk
module is failing!但是我遇到了一个奇怪的错误:包括准系统,默认
aws-sdk
模块失败了!
Short version:精简版:
This code:这段代码:
// Include the AWS SDK module
const AWS = require('aws-sdk');
generates this error: Error: Cannot find module 'aws-sdk'
生成此错误:
Error: Cannot find module 'aws-sdk'
Long Story:很长的故事:
I'm following the process step by step, creating all the pieces using the AWS online tools.我正在逐步遵循该过程,使用 AWS 在线工具创建所有部分。 The first hint of trouble happened when I created my Lambda function.
当我创建 Lambda 函数时,第一个问题出现了。 I wasn't too worried that the Create Function tool shows a Node.js version of 18.x instead of the tutorial's Node.js 16.x, but I definitely noticed that the Lambda shell it generated was named
index.mjs
instead of index.js
.我并不太担心 Create Function 工具显示的是 Node.js 版本 18.x 而不是教程的 Node.js 16.x,但我确实注意到它生成的 Lambda shell 被命名为
index.mjs
而不是index.js
。 No problem, I'll just use that file.没问题,我会使用那个文件。 I replaced the contents with Amazon's HelloWorld code.
我用 Amazon 的 HelloWorld 代码替换了内容。
// 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;
};
I set up the Test Event functionality using their data:我使用他们的数据设置了测试事件功能:
{
"firstName": "Ada",
"lastName": "Lovelace"
}
But running the code generated my first error: exports is not defined in ES module scope
但是运行代码产生了我的第一个错误:
exports is not defined in ES module scope
Doing some searching on that error sent me down a rabbit hole far outside the scope of a basic tutorial, so I renamed the file from index.mjs to index.js.对那个错误进行一些搜索让我陷入了一个远远超出基本教程范围的兔子洞,所以我将文件从 index.mjs 重命名为 index.js。 Rebuilt and poof, it worked just fine:
重建和噗,它工作得很好:
{
"statusCode": 200,
"body": "\"Hello from Lambda, Ada Lovelace\""
}
I won't go through the steps of creating the database, because that's not relevant.我不会完成创建数据库的步骤,因为那不相关。 Everything was fine until I replaced the code with the code that would access the database - it errored with the message above.
一切都很好,直到我用可以访问数据库的代码替换了代码——上面的消息出错了。 To simplify the issue, I deleted the Lambda and rebuilt it with the working code above, and the
require
statement:为了简化问题,我删除了 Lambda 并使用上面的工作代码和
require
语句重新构建它:
// Include the AWS SDK module
const AWS = require('aws-sdk');
// 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;
};
Running that generates this output:运行生成此输出:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
"Require stack:",
"- /var/task/index.js",
"- /var/runtime/index.mjs",
" at _loadUserApp (file:///var/runtime/index.mjs:1000:17)",
" 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"
]
}
The strangest thing I see in the dump: it still references index.mjs
even though I deleted the file (or renamed it - I tried both ways).我在转储中看到的最奇怪的事情:它仍然引用
index.mjs
即使我删除了文件(或重命名它 - 我尝试了两种方法)。 In the online IDE, I only have access to the file tree that shows only index.js.在在线 IDE 中,我只能访问仅显示 index.js 的文件树。 I assume that's
/var/task
so I don't know where /var/runtime
is that still contains a reference to index.mjs
.我假设那是
/var/task
所以我不知道/var/runtime
在哪里仍然包含对index.mjs
的引用。
I'm looking forward to the solution to this head-scratcher.我期待着这个令人头疼的问题的解决方案。 It feels like it's simple enough that it should Just Work!
感觉它很简单,应该可以正常工作!
The code provided by the that tutorial will not work with Lambda Runtime using NodeJS 18. There are a few reasons for this:该教程提供的代码不适用于使用 NodeJS 18 的 Lambda 运行时。这有几个原因:
Node.js 18.x is using AWS SDK v3 . Node.js 18.x 使用AWS SDK v3 。 In your code, you are trying to import AWS SDK v2.
在您的代码中,您正在尝试导入 AWS SDK v2。 The problem is that, NodeJS 18.x runtime does not come with version 2 of the AWS SDK automatically packaged and ready to use.
问题是,NodeJS 18.x 运行时没有自动打包并准备使用的 AWS SDK 版本 2 。 This is the reason why you got the error for not being able to find
aws-sdk
.这就是为什么您收到无法找到
aws-sdk
错误的原因。 The newer v3 SDK is significantly different from the older v2 version.较新的 v3 SDK 与旧的 v2 版本有很大不同。
exports is not defined in ES module scope
- the reason for this error is that Node.js 18.x runtime is using ECMAScript modules instead of CommonJS modules . exports is not defined in ES module scope
- 此错误的原因是 Node.js 18.x 运行时使用的是ECMAScript 模块而不是 CommonJS 模块。 This is why you are seeing a JavaScript file with an .mjs
extension.这就是您看到扩展名为
.mjs
的 JavaScript 文件的原因。 Suffice to say, ECMAScript modules export dependencies differently (using export
keyword), likewise they use import
instead of require
for importing dependencies.可以说,ECMAScript 模块以不同的方式导出依赖项(使用
export
关键字),同样它们使用import
而不是require
来导入依赖项。
Since you are doing a tutorial, I suggest sticking with NodeJS 16 (or lower available version) for now.由于您正在做教程,我建议您暂时坚持使用 NodeJS 16(或可用的更低版本)。 Migrating to NodeJS 18.x is not as a trivial task as you might expect, but is not the end of the world as long as you have the necessary experience.
迁移到 NodeJS 18.x 并不像您预期的那样是一项微不足道的任务,但只要您拥有必要的经验,它就不是世界末日。 Hopefully, this tutorial will be updated in the future for the latest available runtimes.
希望本教程将来会针对最新的可用运行时进行更新。
To create a function using NodeJS 16.x you can select the runtime from the list:要使用 NodeJS 16.x 创建函数,您可以从列表中选择运行时:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.