Cannot find module (AWS Lambda Error) [Solved]

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
4 min

banner

# Table of Contents

  1. Solving the "Cannot find module" AWS Lambda Error
  2. Solve Cannot find Module Lambda Error when using Layers

# Cannot find module (AWS Lambda Error) [Solved]

The "Cannot find module" occurs when a Lambda function is trying to access a module which is not available in the function's execution runtime.

The most common causes for the error are:

  • zipping the wrong files, e.g. zipping a directory instead of the contents of the directory. AWS Lambda expects to extract the zip file and find your handler, not a directory with your handler in it.
  • having a wrong folder structure when using layers. There is a language-specific folder structure you have to follow when using Lambda layers, e.g. nodejs/node_modules for Node.js layers.

This article shows how to solve the error:

  1. When importing modules in a Lambda directly, without layers.
  2. When importing modules in a Lambda function, using layers.
The code for this article is available on GitHub

To solve the "Cannot find module" error when importing modules directly to a Lambda function:

  1. Have the following folder structure:
shell
index.js package.json node_modules your_npm_modules

View the example on Github. The code is in the without-layers directory.

I initialized a package.json file and installed a 3rd party module in the directory of my lambda function's index.js file.

without-layers
npm init -y npm install date-fns@2.24.0

The code in the index.js file imports and uses the module.

The code for this article is available on GitHub
without-layers/index.js
// ๐Ÿ‘‡๏ธ import third party module const {format} = require('date-fns'); exports.handler = async event => { return { statusCode: 200, body: JSON.stringify({ today: format(new Date(), "๐Ÿ‘‰๏ธ 'Today is a' eeee"), }), }; };
  1. Open your terminal in the directory where the index.js file and node_modules directory are located. In this case, the without-layers directory, and zip the contents of the directory
without-layers
zip -r9 lambda.zip .
Note that we are zipping the contents of the directory, and not the directory itself. Once the Lambda service extracts the zip file, it will find our index.js file and the node_modules directory where our third-party module is.
  1. Update the Lambda function's code by uploading the lambda.zip file. Open your terminal in the directory where the lambda.zip file is and run the update-function-code command:
without-layers
aws lambda update-function-code --function-name YOUR_LAMBDA --zip-file fileb://lambda.zip

update function code

  1. Verify that the Lambda function's handler is set to index.handler (an index.js file exporting a handler function):

verify handler configured correctly

  1. Finally, invoke the function and verify that it can access the third-party module.

verify module found

Now Lambda is able to find the third-party module because we've zipped the contents of our index.js file alongside the node_modules directory.

# Solve Cannot find Module Lambda Error when using Layers

To solve the "Cannot find module" error when importing modules using Lambda layers:

  1. Have the following folder structure:
The code for this article is available on GitHub
with-layers
index.js layers date-fns nodejs package.json node_modules
You can view the folder structure in the with-layers directory in the example repository.

Note that the nodejs/node_modules folder structure is what Lambda expects when working with layers in Node.js.

with layers folder structure

  1. Open your terminal in the layers/date-fns/nodejs directory and install your third-party packages:
layers/date-fns/nodejs
npm init -y npm install date-fns@2.24.0

The code for the index.js file looks as follows:

The code for this article is available on GitHub
index.js
// ๐Ÿ‘‡๏ธ import third party package const {format} = require('date-fns'); exports.handler = async event => { return { statusCode: 200, body: JSON.stringify({ today: format(new Date(), "๐Ÿ‘‰๏ธ 'Today is a' eeee"), }), }; };
  1. Open your terminal in the layers/date-fns directory and zip the contents.
When the zip is extracted, the nodejs/node_modules folder structure has to be directly accessible.
layers/date-fns
# in the layers/date-fns directory zip -r9 date-fns-layer.zip .
  1. To create or update the Lambda layer, open your terminal in the layers/date-fns directory and run the publish-layer-version command:
layers/date-fns
aws lambda publish-layer-version --layer-name date-fns-layer --description "add date-fns library" --zip-file fileb://date-fns-layer.zip --compatible-runtimes nodejs10.x nodejs12.x nodejs14.x

create layer

  1. Attach the layer to the Lambda function. The easiest way to add a layer to a function is using the AWS Lambda console.

In the Code tab, scroll down to the Layers section and click Add a layer.

add a layer

Select Custom layers and pick your layer from the dropdown menu. Selecting the latest version and click Add.

select your layer

  1. The last step is to update the code of the Lambda function. In this case the index.js file looks as follows:
The code for this article is available on GitHub
index.js
// ๐Ÿ‘‡๏ธ import third party package const {format} = require('date-fns'); exports.handler = async event => { return { statusCode: 200, body: JSON.stringify({ today: format(new Date(), "๐Ÿ‘‰๏ธ 'Today is a' eeee"), }), }; };

Open your terminal in the directory where the index.js file is located and zip its contents:

Note that we are zipping the index.js file, not the folder that contains it.
with-layers
zip -r9 lambda.zip index.js

zip lambda code

To update the function's code, open your terminal in the directory where the lambda.zip file is and run the update-lambda-code command:

shell
aws lambda update-function-code --function-name YOUR_LAMBDA --zip-file fileb://lambda.zip

update lambda code

  1. Verify that the Lambda function's handler is set to index.handler (an index.js file exporting a handler function):

verify handler configured correctly

  1. Finally, invoke the function and verify that it can access the third-party module.

verify module found

Now lambda is able to find the third-party module because we used the expected directory format for Node.js Lambda layers - nodejs/node_modules.

I've also written a detailed guide on how to use npm modules in AWS Lambda.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev