Last updated: Feb 26, 2024
Reading timeยท4 min

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:
nodejs/node_modules for
Node.js layers.This article shows how to solve the error:
layers.layers.To solve the "Cannot find module" error when importing modules directly to a Lambda function:
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.
npm init -y npm install date-fns@2.24.0
The code in the index.js file imports and uses the module.
// ๐๏ธ 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"), }), }; };
index.js file and
node_modules directory are located. In this case, the without-layers
directory, and zip the contents of the directoryzip -r9 lambda.zip .
index.js file and the node_modules directory where our third-party module is.lambda.zip file. Open
your terminal in the directory where the lambda.zip file is and run the
update-function-code command:aws lambda update-function-code --function-name YOUR_LAMBDA --zip-file fileb://lambda.zip

index.handler (an
index.js file exporting a handler function):

index.js file alongside the node_modules directory.To solve the "Cannot find module" error when importing modules using Lambda layers:
index.js layers date-fns nodejs package.json node_modules
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.

layers/date-fns/nodejs directory and install your
third-party packages:npm init -y npm install date-fns@2.24.0
The code for the index.js file looks as follows:
// ๐๏ธ 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"), }), }; };
layers/date-fns directory and zip the contents.nodejs/node_modules folder structure has to be directly accessible.# in the layers/date-fns directory zip -r9 date-fns-layer.zip .
layers/date-fns directory and run the publish-layer-version command: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

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

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

index.js file looks as follows:// ๐๏ธ 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:
index.js file, not the folder that contains it.zip -r9 lambda.zip index.js

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:
aws lambda update-function-code --function-name YOUR_LAMBDA --zip-file fileb://lambda.zip

index.handler (an
index.js file exporting a handler function):

nodejs/node_modules.I've also written a detailed guide on how to use npm modules in AWS Lambda.
You can learn more about the related topics by checking out the following tutorials: