无法用Nodejs14.x ES模块在AWS lambda中导入软件包

发布于 2025-01-25 01:14:49 字数 568 浏览 5 评论 0 原文

我有一个层,其中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'

它成功了。

你知道为什么吗?如何正确导入包装?

I have a layer where the path of node_modules is nodejs/node14/node_modules.

Using that layer, and I try to import a package in a Lambda function, say 'aws-cloudfront-sign', like this:

import cfsign from 'aws-cloudfront-sign'

I got error message

Cannot find package 'aws-cloudfront-sign' imported from /var/task/signer.js\nDid you mean to import aws-cloudfront-sign/lib/cloudfrontUtil.js?

But if I import the package like this:

import cfsign from '/opt/nodejs/node14/node_modules/aws-cloudfront-sign/lib/cloudfrontUtil.js'

It succeeds.

Do you know why? How could I import the package correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

镜花水月 2025-02-01 01:14:49

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

正如您所做的那样,目前唯一的解决方法似乎是使用绝对路径。例如:

import { DynamoDB } from 'aws-sdk;'

失败,而

import AWS from '/var/runtime/node_modules/aws-sdk/lib/aws.js';
const { DynamoDB } = AWS;

会起作用。

我建议您将自己的声音添加到现有的开放问题中,以帮助确保它引起人们的注意。

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.:

import { DynamoDB } from 'aws-sdk;'

fails, whereas

import AWS from '/var/runtime/node_modules/aws-sdk/lib/aws.js';
const { DynamoDB } = AWS;

will work.

I suggest you add your voice to an existing open issue to help ensure it gets attention.

书信已泛黄 2025-02-01 01:14:49

我遇到了一个类似的问题。我无法在lambda功能中导入我的dynomdb。

我最终做了这样的事情。

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
  DynamoDBDocumentClient,
  PutCommand,
} from "@aws-sdk/lib-dynamodb";

const client = new DynamoDBClient({});

const dynamo = DynamoDBDocumentClient.from(client);

这将创建一个新客户端,您可以用来运行各种命令。

请参阅此处的文档: 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.

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
  DynamoDBDocumentClient,
  PutCommand,
} from "@aws-sdk/lib-dynamodb";

const client = new DynamoDBClient({});

const dynamo = DynamoDBDocumentClient.from(client);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文