无法用Nodejs14.x ES模块在AWS lambda中导入软件包
我有一个层,其中node_modules的路径为 nodejs/node14/node_modules
。
使用该层,我尝试在lambda函数中导入一个软件包,例如“ AWS-CloudFront-Sign”,例如:
import cfsign from 'aws-cloudfront-sign'
我收到了错误消息,
Cannot find package 'aws-cloudfront-sign' imported from /var/task/signer.js\nDid you mean to import aws-cloudfront-sign/lib/cloudfrontUtil.js?
但是如果我将其导入这样的软件包:
import cfsign from '/opt/nodejs/node14/node_modules/aws-cloudfront-sign/lib/cloudfrontUtil.js'
它成功了。
你知道为什么吗?如何正确导入包装?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2023更新:
请注意,根据问题,下面的解决方案是针对特定SDK版本的节点14问题。 现在在节点18+中支持AWS SDK V3的进口。将以下解决方案用于其他节点/SDK版本可能无法正常工作。
参见 aws post href =“ https://github.com/vibe/aws-esm-modules-layer-support” rel =“ noreferrer”>当使用lambda的节点Runtime 14和16 时,不可用Node_path的ESM导入。
结束更新
这似乎是一个错误。它发生在层和SDK中。 GitHub上有许多类似的开放问题:
“ noreferrer”> nodejs lambda:找不到软件包'aws-sdk'
在使用ES模块时找不到软件包和lambda layer
es6 imports在 @aws--- SDK/client-iotsitewise
正如您所做的那样,目前唯一的解决方法似乎是使用绝对路径。例如:
失败,而
会起作用。
我建议您将自己的声音添加到现有的开放问题中,以帮助确保它引起人们的注意。
2023 Update:
Note that the solution below was for a Node 14 problem with a specific SDK version, per the question. Imports for AWS SDK v3 are now supported in Node 18+. Using the below solution for other Node/SDK versions likely will not work.
See the AWS post here and the discussion ESM imports from NODE_PATH are not available when using Lambda's Node Runtime 14 and 16.
End update
This appears to be a bug. It is occurring with layers and the SDK. There are are a number of similar open issues on Github:
Nodejs Lambda: Cannot find package 'aws-sdk'
Cannot find package when using ES Module and Lambda Layer
ES6 imports don't work in @aws-sdk/client-iotsitewise
As you have worked out, the only workaround at present seems to be the use of absolute paths. E.g.:
fails, whereas
will work.
I suggest you add your voice to an existing open issue to help ensure it gets attention.
我遇到了一个类似的问题。我无法在lambda功能中导入我的dynomdb。
我最终做了这样的事情。
这将创建一个新客户端,您可以用来运行各种命令。
请参阅此处的文档: javascript dynamodb
查看这些客户文档的DynamoDB
I was running across a similar issue. I wasn't able to import my Dynomdb in my lambda function.
I ended up doing something like this.
This will create a new client that you can use to run the various commands.
See documentation here: AWS SDK for JavaScript DynamoDB
Check out these Client Documentation for DynamoDB enter link description here