typescriptwebpackserverlessaws-serverlessaws-sdk-js-v3

export 'S3Client' (imported as 'S3Client') was not found in '@aws-sdk/client-s3'


I am using the serverless framework and create a TypeScript based serverless API and trying to work with the AWS JavaScript v3 SDK.

Currently I have this in my package.json:

  "dependencies": {
    "@aws-sdk/client-s3": "^3.367.0",
    "@aws-sdk/client-sns": "^3.32.0",
  }

And a simple file that uses the S3 client functionality looks like this (referenced from aws code examples):

import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3'

const client = new S3Client({})

export const s3Upload = async (bucket: string, key: string, body: string) => {
  const command = new PutObjectCommand({
    Bucket: bucket,
    Key: key,
    Body: body,
  })

  const response = await client.send(command)
  console.log('s3 upload response', response)
  return response
}

However, when trying to invoke one of my serverless functions locally with the sls invoke local command, I am getting this error:

Error:
Webpack compilation failed:

in ../../node_modules/@aws-sdk/client-s3/dist-es/index.js 2:0-27
  Module not found: Error: Can't resolve './S3Client' in '/*/*/*/*/*/node_modules/@aws-sdk/client-s3/dist-es'

When I actually navigate to the directory in node_modules, I see this in the index.js

export * from "./S3";
export * from "./S3Client";
export * from "./commands";
export * from "./models";
export * from "./pagination";
export * from "./waiters";
export { S3ServiceException } from "./models/S3ServiceException";

So does this mean there is something not going right in the compilation of the dependency? The odd thing is, the exact same code works perfectly for the SNS client.

Has anyone ever seen this or know how to fix it?


Solution

  • Was able to figure out the solution. In my webpack config, I had this in the resolve object.

      resolve: {
        extensions: ['.mjs', '.json', '.ts'],
        symlinks: false,
        cacheWithContext: false,
      },
    

    However, I realized that AWS ships just plain old javascript in their v3 SDK. So, after adding .js to the list of extensions, the compiling worked just fine.