# AWS Lambda Layers :: How to add and require a simple js file from a lambda layer

**URL:** <https://forum.serverless.com/t/aws-lambda-layers-how-to-add-and-require-a-simple-js-file-from-a-lambda-layer/8720>\
**Category:** Serverless Architectures\
**Created:** [July 5, 2019, 8:37pm UTC](https://forum.serverless.com/t/aws-lambda-layers-how-to-add-and-require-a-simple-js-file-from-a-lambda-layer/8720 "2019-07-05T20:37:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev.danielgusmao](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/dev.danielgusmao/32/3017_2.png) [@dev.danielgusmao](https://forum.serverless.com/u/dev.danielgusmao)\
**Post date:** [July 5, 2019, 8:37pm UTC](https://forum.serverless.com/t/aws-lambda-layers-how-to-add-and-require-a-simple-js-file-from-a-lambda-layer/8720/1 "2019-07-05T20:37:56Z")

</div>

In my serverless project, I have some js files that are being repeated among several lambdas functions, and I want to add theses files inside my lambda layer and use them as a simple require() inside each lambda function.

I am already using a lambda layer to share npm packages among lambdas, and it is working as expected, with the structure: nodejs/node\_modules/{package-name}/

But I dont want to transform my simple files into a private npm package to make it work.

I’ve tried to create a new folder inside nodejs folder, called /custom\_libs

So my attempt is nodejs/custom\_libs/lib-name.js and the require inside lambda function is:

require("/opt/custom\_libs/lib-name.js");

But it is not working.

Any thoughts on how can I achieve this ?

Thanks

---

<div class="post-metadata">

**Author:** ![KanesYang](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/kanesyang/32/3087_2.png) [@KanesYang](https://forum.serverless.com/u/KanesYang)\
**Post date:** [August 21, 2019, 6:38pm UTC](https://forum.serverless.com/t/aws-lambda-layers-how-to-add-and-require-a-simple-js-file-from-a-lambda-layer/8720/2 "2019-08-21T18:38:37Z")

</div>

Hi Daniel,

I met the same issue as yours. Have you found the solution yet? thanks.

Best.

---

<div class="post-metadata">

**Author:** ![dev.danielgusmao](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/dev.danielgusmao/32/3017_2.png) [@dev.danielgusmao](https://forum.serverless.com/u/dev.danielgusmao)\
**Post date:** [August 22, 2019, 12:28am UTC](https://forum.serverless.com/t/aws-lambda-layers-how-to-add-and-require-a-simple-js-file-from-a-lambda-layer/8720/3 "2019-08-22T00:28:02Z")

</div>

Hi Kanes!

Yes, I found a solution, but not using lambda layers.

I kept my custom libs in a folder inside my project and referred to them using relative paths (eg: require(’…/…/…/custom\_libs/myLib’).

Later I got another approach, setting a constant with the absolute path for Node that is ‘/var/task’ and appended them in the require path.

Note that you need to use serverless package commands, include and exclude , to insert this folder inside each lambda. Or use webpack as well to do this kind of job.

Cya!

---

<div class="post-metadata">

**Author:** ![KanesYang](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/kanesyang/32/3087_2.png) [@KanesYang](https://forum.serverless.com/u/KanesYang)\
**Post date:** [August 22, 2019, 1:45pm UTC](https://forum.serverless.com/t/aws-lambda-layers-how-to-add-and-require-a-simple-js-file-from-a-lambda-layer/8720/4 "2019-08-22T13:45:28Z")

</div>

Hi Daniel!

Thanks for your helpful response! I am actually using the first solution of your response. One thing makes me want to switch to Lambda Layer is that the deployment take toooooo long! I will try the second approach later for comparison. Anyway, thank you very much!!!

Best.

---

<div class="post-metadata">

**Author:** ![dev.danielgusmao](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/dev.danielgusmao/32/3017_2.png) [@dev.danielgusmao](https://forum.serverless.com/u/dev.danielgusmao)\
**Post date:** [August 22, 2019, 2:43pm UTC](https://forum.serverless.com/t/aws-lambda-layers-how-to-add-and-require-a-simple-js-file-from-a-lambda-layer/8720/5 "2019-08-22T14:43:13Z")

</div>

Yes, I suppose that the command package, include/exclude, that takes longer to process. Maybe if you use webpack to bundle include/exclude files, this process will be reduced.

Regards,  
Daniel
