6

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

10

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.

5
  • 1
    Thank you! I have opened a ticket to ask about this. Hope they fix it soon.
    – Lam
    Apr 30, 2022 at 14:33
  • 6
    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", Dec 14, 2022 at 21:49
  • Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/runtime/node_modules/aws-sdk/lib/aws.js' imported from /var/task/index.mjs May 28 at 18:28
  • The 2023 update section helped me. I was upgrading from node 16.x to 18.x runtime for it to work.
    – fsevenm
    Jul 3 at 23:35
  • 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
    – YoniXw
    Aug 31 at 19:44
0

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.