是什么导致此 Lambda 在 aws-sdk 版本 3 中抛出错误?

What is Causing this Lambda to Throw an Error with aws-sdk Version 3?

我有一个由 SNS 队列触发的 AWS lambda 函数,它应该从它收到的消息中提取一些数据,对其进行格式化,然后将其发送到不同的 SNS 队列。在我尝试发送到第二个队列之前,一切都运行良好。为此,我安装了 @aws-sdk/client-sns,并尝试发送一条简单的消息。当我 运行 代码时,它抛出以下与无法找到 TypeScript 定义相关的错误:

✖ in ./node_modules/@aws-sdk/client-sns/dist-es/index.js 1:0-22
    Module not found: Error: Can't resolve './SNS' in '/Users/userName/development/aws-sdk-error/node_modules/@aws-sdk/client-sns/dist-es'
    resolve './SNS' in '/Users/userName/development/aws-sdk-error/node_modules/@aws-sdk/client-sns/dist-es'
      using description file: /Users/userName/development/aws-sdk-error/node_modules/@aws-sdk/client-sns/package.json (relative path: ./dist-es)
        using description file: /Users/userName/development/aws-sdk-error/node_modules/@aws-sdk/client-sns/package.json (relative path: ./dist-es/SNS)
          no extension
            /Users/userName/development/aws-sdk-error/node_modules/@aws-sdk/client-sns/dist-es/SNS doesn't exist
          .ts
            /Users/userName/development/aws-sdk-error/node_modules/@aws-sdk/client-sns/dist-es/SNS.ts doesn't exist
          tsx
            /Users/userName/development/aws-sdk-error/node_modules/@aws-sdk/client-sns/dist-es/SNStsx doesn't exist
          as directory
            /Users/userName/development/aws-sdk-error/node_modules/@aws-sdk/client-sns/dist-es/SNS doesn't exist

这是抛出错误的文件的简化版本:

import { SNSClient, PublishCommand } from '@aws-sdk/client-sns';

export const showError = async () => {

  const client = new SNSClient( { region: 'us-west-1' } );

  const params = {
    Message: 'some text',
    TopicArn: 'arn:aws:sns:us-east-1:111111111111:someOtherTopic'
  };

  try {
    await client.send( new PublishCommand( params ));
  } catch ( error ) {
  }
};

虽然我已经尝试更改 tsconfig.json 文件中的值并尝试了多个版本的 Node (14.x, 16.x, 18.x), 但想使用 16.15.0。任何解决这个问题的帮助将不胜感激。

我在 Github 上创建了一个示例存储库,显示了操作中的错误:https://github.com/autoboxer/aws-sdk-error

看起来 @aws-sdk/client-sns/dist-es 中的模块是从 vanilla JS 文件中导出的,根据您在 webpack 配置中配置模块解析的方式,这些文件可能不会被提取。

尝试将 .js 添加到您的扩展数组:

resolve: {
    extensions: [ '.ts', '.tsx', '.js' ]
}

这应该允许 webpack 解析 @aws-sdk/client-sns 中的 vanilla JS 模块,并有望解决这个问题。