Skip to content

How do I resolve "Cannot find module" or "Cannot find Package" errors when I run Lambda code in Node.js?

3 minute read
1

I receive "Cannot find module" or "Cannot find Package" errors when I run AWS Lambda code in Node.js.

Short description

If your Lambda environment can't find the specified library in the Node.js code, then you receive one of the preceding errors. These errors occur because Lambda isn't pre-packaged with all Node.js libraries.

Resolution

To resolve these errors, create a Lambda layer that includes the libraries that you want to use in your Node.js code. You can reuse the layer across multiple Lambda functions.

For Node.js runtimes 16 and earlier, Lambda doesn't support layered JavaScript ES module dependencies. You must include the dependencies in the deployment. Lambda supports JavaScript ES module dependencies for Node.js 24

Each Lambda runtime adds specific /opt directory folders to the PATH variable. If the layer uses the same folder structure, then your Lambda function's code can access the layer content without a specific path.

Important: The library that you import for Node.js must be inside the nodejs/node_modules folder structure.

It's a best practice to create a Lambda layer on the same operating system (OS) that your Lambda supported runtime is based on. For example, the latest versions of Node.js are based on the Amazon Linux 2023 Amazon Machine Images (AMIs).

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

To create a Lambda layer for a Node.js library, complete the following steps:

  1. Create an Amazon Elastic Compute Cloud (Amazon EC2) instance from an Amazon Linux 2023 AMI.

  2. Create an AWS Identity and Access Management (IAM) role with permissions to call the publish-layer-version API.

  3. Attach the IAM role to the Amazon EC2 instance.

    Note: Your instance now has permissions to upload Lambda layers for the publish-layer-version API call.

  4. Use SSH to connect to your EC2 instance.

  5. To install Node.js, run the following curl command:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh
    bash
    $ . ~/.nvm/nvm.sh
    nvm install 24
  6. Create a nodejs folder:

    mkdir nodejs
    
  7. Install the aws-xray-sdk library into the nodejs folder:

    cd nodejs$ npm init  ***answer all queries for initialization that create a package.json file***
    npm install --save aws-xray-sdk
    

    Note: Replace the aws-xray-sdk with the Node.js library that you want to import.

  8. Create a zip archive for the aws-xray-sdk library:

    zip -r layer.zip nodejs
    

    Note: The aws-xray-sdk library is in the required folder format for a Node.js layer nodejs/node_modules.

  9. To publish the Lambda layer, run the following publish-layer-version command:

    aws lambda publish-layer-version --layer-name xray --zip-file fileb://layer.zip --compatible-runtimes nodejs24.x --region your-region
    

    Note: Replace xray with your layer's name, nodejs24.x with your Node.js version, and your-region with your AWS Region.

To add modules ECMAScript 6 (ES6) or CommonJS in your code, see JavaScript ES6/CommonJS syntax.

Related information

How do I troubleshoot ""permission denied"" or ""unable to import module"" errors when I upload a Lambda deployment package?

How do I build a Lambda deployment package for Node.js?

AWS OFFICIALUpdated 7 days ago