10

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?

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! I have opened a ticket to ask about this. Hope they fix it soon.
I have also run into this issue, the above solution just gave me this error: "errorType": "Error", "errorMessage": "Cannot find module '/var/runtime/node_modules/aws-sdk/lib/aws.js' imported from /var/task/index.mjs",
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/runtime/node_modules/aws-sdk/lib/aws.js' imported from /var/task/index.mjs
The 2023 update section helped me. I was upgrading from node 16.x to 18.x runtime for it to work.
What I didn't get is that in v3 they split the sdk to help with build size, so you will need to import based on the service you use. Use the docs: docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/introduction
6

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.