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

Time:05-17

I have an AWS lambda function triggered by an SNS queue, which is supposed to extract some data from the message it receives, format it, then send it to a different SNS queue. Everything was working well until I tried sending to the second queue. In order to do so, I installed @aws-sdk/client-sns, and attempted to send a simple message. When I run the code, it's throwing the following error related to not being able to find TypeScript definitions:

✖ 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

This is the simplified version of the file throwing the error:

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 ) {
  }
};

I've been searching for hours and haven't been able to pinpoint the issue, although I've tried changing values in my tsconfig.json file and have tried multiple versions of Node (14.x, 16.x, 18.x), but would like to use 16.15.0. Any help figuring this out would be greatly appreciated.

I've created a sample repo on Github showing the error in action: https://github.com/autoboxer/aws-sdk-error

CodePudding user response:

It looks like the modules within @aws-sdk/client-sns/dist-es are exported from vanilla JS files, which may not be picked up based on how you're configuring module resolution in your webpack config.

Try adding .js to your extensions array:

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

That should allow webpack to resolve the vanilla JS modules within @aws-sdk/client-sns, and will hopefully resolve this issue as well.

  • Related