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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With