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

There are 2 ways to use npm modules in a Lambda function:
zip the node_modules directory alongside your index.js handler
file - very easy and straightforward.50 MB, so if your npm modules exceed that, you have to use layersThis article covers how to use npm modules both with and without layers.
without-layers directory in the GitHub repository.To use npm modules in a Lambda function, you have to:
index.js package.json node_modules your_npm_modules
First, initialize the package.json file and install the necessary modules, in
the directory where the index.js file is located:
npm init -y npm install date-fns@2.24.0
Let's fill out the code for the index.js file, which makes use of the npm
module:
// ๐๏ธ import npm module const {format} = require('date-fns'); exports.handler = async event => { return { statusCode: 200, body: JSON.stringify({ // ๐๏ธ use npm module today: format(new Date(), "๐๏ธ 'Today is a' eeee"), }), }; };
index.js and node_modules are
and zip the folder's contents:zip -r9 lambda.zip .
index.js file, not a directory/index.js file.lambda.zip file.
Open your terminal in the directory where the lambda.zip file is located
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 function named handler):

index.js file and the node_modules directory our Lambda function can use the npm modules in it.However, now we can't use the online editor. Our deployment package is too
large, due to the size of the npm_modules folder.
Also, what if we need a particular npm package in multiple functions? It
doesn't seem very efficient to repeat this process for every function.
This is where Lambda layers come in.
with-layers directory of the GitHub repository.To use npm modules, with layers, in a Lambda function, you have to:
index.js layers date-fns nodejs package.json node_modules
Note that the nodejs/node_modules folder structure is what Lambda expects when
working with layers in Node.js -
source.

layers/date-fns/nodejs directory and install your
npm modules:npm init -y npm install date-fns@2.24.0
layers/date-fns directory and zip the contents.nodejs/node_modules directory 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 drop-down menu, selecting
the latest version.

index.js file:// ๐๏ธ import npm package const {format} = require('date-fns'); exports.handler = async event => { return { statusCode: 200, body: JSON.stringify({ // ๐๏ธ use npm package today: format(new Date(), "๐๏ธ 'Today is a' eeee"), }), }; };
The code simply imports the npm module from the layer and makes use of it.
Open your terminal in the directory where the index.js file is located and zip
the file.
index.js file, not the folder that contains it.zip -r9 lambda.zip index.js

Finally, let's update the function's code. Open your terminal in the directory
where the lambda.zip file is located 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 function named handler):

The node_modules directory is not visible in the online code editor and now we
can view the code of our Lambda function.

npm modules or even your own helper functions that you reuse between your lambdas.You can learn more about the related topics by checking out the following tutorials: