Solve - Cannot find module (AWS Lambda Error)

avatar

Borislav Hadzhiev

Wed Sep 29 20214 min read

banner

Photo by Arya Praditya

Table of Contents #

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

Solving the "Cannot find module" AWS Lambda Error #

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've first 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 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 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. Finally 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 like:

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 drop down 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 like:
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

Finally 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've used the expected directory format for Node.js lambda layers - nodejs/node_modules.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee