Search code examples
node.jsamazon-web-servicesaws-lambdaimportamazon-dynamodb

Lambda function error on importing AWS in Nodejs


I'm trying to import the AWS into the Lambda function, and use that to add a new object to a DynamoDB table. (Nodejs version: 18)

I tried many options to import the AWS:

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

But neither of them worked out for me, and every time it got me a message similar to this: "Cannot find package '<package_name>' imported from "

(or for the "require" option, I've got "require is not defined in ES module scope, you can use import instead")


Solution

  • Use:

    import { DynamoDB } from '@aws-sdk/dynamodb';

    But also rename your index.js file to index.mjs, that should do the trick.