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

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

Short descriptionIf your Lambda environment can't find the specified library in the Node.js code, then you receive one of these errors. These errors occur because Lambda isn't prepackaged with all Node.js libraries.If a module is missing with CommonJS, then you receive the following error: Runtime.ImportModuleError: Error: Cannot find module.If a module is missing with JavaScript ES6, then you receive the following error: Error [ERR_MODULE_NOT_FOUND]: Cannot find package.To resolve these errors, create a deployment package or Lambda layer that includes the libraries that you want to use in your Node.js code.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 18.Use the following steps to create a Lambda layer. Create a Lambda layer instead of a deployment package so that you can reuse the layer across multiple Lambda functions. 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 specifying the path.Important: The library that you import for Node.js must be inside the nodejs/node_modules folder structure.

ResolutionIt's a best practice to create a Lambda layer on the same operating system that your Lambda runtime is based on. For example, all versions of Node.js are based on the Amazon Linux 2 Amazon Machine Images (AMI).Create a Lambda layer for a Node.js library:1.    Create an Amazon Elastic Compute Cloud (Amazon EC2) instance from an Amazon Linux 2 AMI.2.    Create an AWS Identity and Access Management (IAM) role with permissions to call the publish-layer-version API. Then, attach the IAM role to the EC2 instance.Note: Your EC2 instance now has permissions to upload Lambda layers for the publish-layer-version API call.3.    Connect to your EC2 instance, and then install Node.js:$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash$ . ~/.nvm/nvm.sh$ nvm install node4.    Create a nodejs folder:$ mkdir nodejs5.    Install the aws-xray-sdk library into the nodejs folder:$ cd nodejs$ npm init  ***answer all queries regarding initialization that create a package.json file***$ npm install --save aws-xray-sdkNote: Replace the aws-xray-sdk example library with the Node.js library that you want to import.6.    Create a zip archive for the aws-xray-sdk library:$ zip -r layer.zip nodejsNote: The aws-xray-sdk library is in the required folder format for a Node.js layer: nodejs/node_modules.7.    Publish the Lambda layer:$ aws lambda publish-layer-version --layer-name xray --zip-file fileb://layer.zip --compatible-runtimes nodejs12.x --region us-east-1Note: To add modules ECMAScript 6 (ES6) or CommonJS in your code read, see JavaScript ES6/CommonJS syntax.

Related informationHow do I troubleshoot "permission denied" or "unable to import module" errors when uploading a Lambda deployment package?