# AWS Lambda Layers - Node Modules not working

**URL:** <https://forum.serverless.com/t/aws-lambda-layers-node-modules-not-working/7914>\
**Category:** Serverless Framework\
**Tags:** aws, lambda\
**Created:** [April 5, 2019, 4:44pm UTC](https://forum.serverless.com/t/aws-lambda-layers-node-modules-not-working/7914 "2019-04-05T16:44:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iq-dot](https://avatars.discourse-cdn.com/v4/letter/i/b782af/32.png) [@iq-dot](https://forum.serverless.com/u/iq-dot)\
**Post date:** [April 5, 2019, 4:44pm UTC](https://forum.serverless.com/t/aws-lambda-layers-node-modules-not-working/7914/1 "2019-04-05T16:44:07Z")

</div>

Hi all,

I am trying to use AWS Lambda Layers to make the node\_modules folder a common layer as I have multiple functions that rely on it.

I have successfully created a layer and linked the layer to a function, however when I invoke it, it gives me an error that a module (in node modules) that it tried to require does not exist. Which means the layer isn’t working or somehow the function can’t find the right path.

I checked the layer by downloading and all my modules are there. I also checked the function and it has the layer assigned to it.

Here my yml file sections:

```
layers:
  analyticsShared:
    path: node_modules
    name: analytics-shared-${opt:stage, self:provider.stage}
    description: "Shared node modules for analytics"

functions:
  track:
    handler: analytics.queue
    timeout: 30
    layers:
      - {Ref: AnalyticsSharedLambdaLayer}

```

What am I doing wrong? Can someone please help me.

---

<div class="post-metadata">

**Author:** ![snolangps](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/snolangps/32/2582_2.png) [@snolangps](https://forum.serverless.com/u/snolangps)\
**Post date:** [April 30, 2019, 9:30pm UTC](https://forum.serverless.com/t/aws-lambda-layers-node-modules-not-working/7914/2 "2019-04-30T21:30:21Z")

</div>

Do you need to provide `compatibleRuntimes`? Or when referencing the `node_modules` now, are you going through `/opt/layerName/node_modules`?

---

<div class="post-metadata">

**Author:** ![FanFataL](https://avatars.discourse-cdn.com/v4/letter/f/a5b964/32.png) [@FanFataL](https://forum.serverless.com/u/FanFataL)\
**Post date:** [May 28, 2019, 4:37pm UTC](https://forum.serverless.com/t/aws-lambda-layers-node-modules-not-working/7914/3 "2019-05-28T16:37:18Z")

</div>

You need to insert your node\_modules directory into nodejs directory  
└ nodejs/node\_modules  
[https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path)

---

<div class="post-metadata">

**Author:** ![ken](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@ken](https://forum.serverless.com/u/ken)\
**Post date:** [June 24, 2021, 8:01am UTC](https://forum.serverless.com/t/aws-lambda-layers-node-modules-not-working/7914/4 "2021-06-24T08:01:02Z")

</div>

```auto
layers:
  AnalyticsShared:
    path: node_modules
    name: analytics-shared-${opt:stage, self:provider.stage}
    description: "Shared node modules for analytics"

functions:
  track:
    handler: analytics.queue
    timeout: 30
    layers:
      - {Ref: AnalyticsSharedLambdaLayer}

```

This way of writing is correct.“ **AnalyticsShared:** ”  
This part doesn’t always work correctly though Serverless Framework says as following in [AWS - Layers:Using your layers](https://www.serverless.com/framework/docs/providers/aws/guide/layers#using-your-layers).

> To use a layer with a function in the same service, use a CloudFormation Ref. The name of your layer in the CloudFormation template will be your layer name [TitleCased](https://en.wikipedia.org/wiki/Letter_case#Title_Case) (without spaces) and have `LambdaLayer` appended to the end. EG:

- wrong

```auto
layers:
  test:
    path: layer
functions:
  hello:
    handler: handler.hello
    layers:
      - { Ref: TestLambdaLayer }

```

- right

```auto
layers:
  Test:
    path: layer
functions:
  hello:
    handler: handler.hello
    layers:
      - { Ref: TestLambdaLayer }

```

This phenomenon may happen when layers name is equal to service name. That’s because layers name create zip file (e.g.Test.zip) as well as service name create zip file (e.g.test.zip) when sls deploy with cloudformation info.

---

<div class="post-metadata">

**Author:** ![BabbRust](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@BabbRust](https://forum.serverless.com/u/BabbRust)\
**Post date:** [July 1, 2021, 6:23am UTC](https://forum.serverless.com/t/aws-lambda-layers-node-modules-not-working/7914/5 "2021-07-01T06:23:35Z")

</div>

Hi…At the point when you make a layer, you should package all it’s anything but a .compress record document. You transfer the .compress record file to your layer from Amazon Simple Storage Service (Amazon S3) or your neighborhood machine. Lambda separates the layer substance into the/pick registry when setting up the execution climate for the capacity.
