Kien shows you how to
build a Lambda deployment package
for Node.js

kien_lambda_node

I created an AWS Lambda function in Node.js, but I get an error during invocation. How do I build my deployment package so that I don't get errors?

A common error with Lambda functions in Node.js is "cannot find module." This error is usually caused when your deployment package doesn't have the correct folder structure for the Lambda service to load your modules and libraries, or it has incorrect file permissions. (Lambda requires global read permissions.)

Follow these instructions to build a deployment package that includes the function code in the root of the .zip file with read and execute permissions for all files.

Linux and macOS

Install dependencies to the function project folder

Note: Most Node.js modules are platform-independent, but some modules are compiled against specific operating system environments. Lambda runs under a Linux environment. When installing modules with npm, we recommend building the .zip file in a Linux environment to be sure that the correct platform dependencies are included.

1.    In the command line interface (CLI), change directories to your project folder. For example:

cd /project-folder-name

Note: Replace project-folder-name with the actual name of your project folder.

2.    Install your dependencies locally to your function project folder using the command npm install package-name, replacing package-name with the actual package name. For example, to install the AWS SDK for JavaScript modules to the root of your project folder, run this command:

npm install aws-sdk

Tip: Lambda includes the AWS SDK for JavaScript libraries as part of the base container configuration. But if you need to use a different version of the library, you can also include a local copy. For more information about the current Lambda environment and supported libraries, see Lambda Execution Environment and Available Libraries.

Run the command for each module required for your Lambda function.

Note: There's a 250-MB limit on your function size for unzipped files. Only include the libraries you need for your function to work.

Build the deployment package

In the CLI, run this command:

zip -r ../function-name.zip .

Note: Replace function-name with the file name you want to give your deployment package.

This puts all files in the project folder into a .zip file located in the parent folder.

Verify the deployment package

1.    In the CLI, run this command:

zipinfo ../function-name.zip

Note: Replace function-name with the actual file name of your deployment package.

You can also run unzip -l on the .zip file to list its contents, though the output won't be as detailed.

2.    Check the output to verify that the function handler source file is located in the root of the .zip file.

3.    Check the output to verify that your files have global read permissions. For more information, including how to fix permissions if needed, see Permissions Policies on Lambda Deployment Packages.

Windows

Build the deployment package

1.    In File Explorer, open your project folder.

2.    Select all of the project files, then right-click to open the context menu.

3.    Choose Send to, then choose Compressed (zipped) folder.

4.    Enter a name for the .zip file.

Upload and verify the deployment package

1.    In the Lambda console, choose your function.

2.    Under Function code, for Code entry type, choose Upload a .zip file.

3.    Under Function package, choose Upload.

4.    Choose the .zip file you created, and then choose Open.

5.    At the top of the console, choose Save.

Tip: You can also run update-function-code from the AWS Command Line Interface (AWS CLI) to upload your .zip file.

6.    After uploading is finished, choose Test.

Tip: You can also use 7-Zip from the AWS CLI to verify your deployment package's file permissions. Download it from the 7-Zip website. For more information, see Permissions Policies on Lambda Deployment Packages.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2018-06-29

Updated: 2019-02-15