# require('package') in Serverless functions

**URL:** <https://community.hubspot.com/t/require-package-in-serverless-functions/78247>\
**Category:** CMS Development\
**Tags:** local-development\
**Created:** [March 29, 2023, 4:24am UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247 "2023-03-29T04:24:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JBrake](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jbrake/32/76513_2.png) [@JBrake](https://community.hubspot.com/u/JBrake)\
**Post date:** [March 29, 2023, 4:24am UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/1 "2023-03-29T04:24:03Z")

</div>

Greetings Hublers…

Ive add the crypto-js package to my project:  
npm install -g crypto-js

Added the dependencies to package.json:

“dependencies”: {

```
"crypto-js": "^4.1.1"

```

},

I can see it min my package-lock.json:

```
"crypto-js": {

  "version": "4.1.1",

  "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.1.1.tgz",

  "integrity": "..."

},

```

However when I “require” the package in my serverless function file, the package is not found: “‘crypto-js’ should be listed in the project’s dependencies.” (seams to find the file in the prompt? weird its not then found)

![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/1/7/17131cf5a6f815998738484d3ba28c3f430b798b.png)

So of coarse when I run the code, the file logs it confirms not finding the package:

- UNHANDLED\_ERROR - Execution Time: 2334msRuntime.ImportModuleError: Error: Cannot find module ‘crypto-js’

Has anyone else experienced this issue? managed to resolve it? or perhaps I’ve missed something in my adding of the package.

Assistance appreciated.

Thanks,

Jonathan

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [March 29, 2023, 6:59pm UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/2 "2023-03-29T18:59:56Z")

</div>

Hi, @JBrake 👋 Thank you for sharing your question. Let’s see if we can get the conversation going for you — hey @Teun @louischausse [@Mike\_Eastwood](https://community.hubspot.com/t5/user/viewprofilepage/user-id/9259), can you lend a hand here?

Thank you very much! — Jaycee

---

<div class="post-metadata">

**Author:** ![JBrake](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jbrake/32/76513_2.png) [@JBrake](https://community.hubspot.com/u/JBrake)\
**Post date:** [March 29, 2023, 7:26pm UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/3 "2023-03-29T19:26:31Z")

</div>

Thanks [@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074), I can’t be the first one to have this problem!

---

<div class="post-metadata">

**Author:** ![Mike\_Eastwood](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mike_eastwood/32/70075_2.png) [@Mike\_Eastwood](https://community.hubspot.com/u/Mike_Eastwood)\
**Post date:** [March 30, 2023, 12:24am UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/4 "2023-03-30T00:24:48Z")

</div>

Hey @JBrake

As far as I’m aware we can’t install packages in the HubSpot CMS – we’re limited to the ones HubSpot has preinstalled.

I’d be excitied to know if that’s changed recently?

Mike

---

<div class="post-metadata">

**Author:** ![JBrake](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jbrake/32/76513_2.png) [@JBrake](https://community.hubspot.com/u/JBrake)\
**Post date:** [March 30, 2023, 12:43am UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/5 "2023-03-30T00:43:03Z")

</div>

Yes I’m getting that feeling too [@Mike\_Eastwood](https://community.hubspot.com/t5/user/viewprofilepage/user-id/9259) . In my google search I found a work aound but it no longer works as it folder is now alot more locked down. However [here](https://developers.hubspot.com/docs/cms/data/serverless-functions/reference#preloaded-packages) (bottom) it mentions webpacks, but I need an existing package from npm.

---

<div class="post-metadata">

**Author:** ![brm013](https://avatars.discourse-cdn.com/v4/letter/b/ce73a5/32.png) [@brm013](https://community.hubspot.com/u/brm013)\
**Post date:** [December 22, 2023, 3:33am UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/6 "2023-12-22T03:33:49Z")

</div>

Any movement on this? Would love to know how to require external packages into a serverless function. None of the examples around work, all too old I suspect. Very frustrating

---

<div class="post-metadata">

**Author:** ![JBrake](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jbrake/32/76513_2.png) [@JBrake](https://community.hubspot.com/u/JBrake)\
**Post date:** [December 26, 2023, 7:43pm UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/7 "2023-12-26T19:43:23Z")

</div>

Best I’ve managed to to is put my required packages in to a separate project to render and take the dist output, find the place I need to add my code (marked by code in the code to be rendered) and then upload that as a serverless function… apart from the messness of this the (mostly minimised) code is not shared across multiple files and is often over the 170kb recommeded limit for serverless funcitons.

---

<div class="post-metadata">

**Author:** ![brm013](https://avatars.discourse-cdn.com/v4/letter/b/ce73a5/32.png) [@brm013](https://community.hubspot.com/u/brm013)\
**Post date:** [January 10, 2024, 11:43pm UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/8 "2024-01-10T23:43:53Z")

</div>

I managed to get something working. I found this example [GitHub - HubSpot/cms-webpack-serverless-boilerplate: Boilerplate for bundling serverless functions with webpack locally, prior to uploading to the CMS. · GitHub](https://github.com/HubSpot/cms-webpack-serverless-boilerplate). One of the functions in there imports ‘lodash’. I had found this earlier but it didn’t work for importing the modules that I wanted but it turns out this issue I was having was a version of node issue. I was trying to use node 18.18.1 but I tried node 16.20.2 and it worked. I haven’t had time to play around with other versions yet. I’ve even managed to get TypeScript to work too.

---

<div class="post-metadata">

**Author:** ![Rahul\_Sharma](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@Rahul\_Sharma](https://community.hubspot.com/u/Rahul_Sharma)\
**Post date:** [July 31, 2024, 11:27am UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/9 "2024-07-31T11:27:07Z")

</div>

hey Did you find any solution for add packages into serverless functions ?

---

<div class="post-metadata">

**Author:** ![JBrake](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jbrake/32/76513_2.png) [@JBrake](https://community.hubspot.com/u/JBrake)\
**Post date:** [July 31, 2024, 8:24pm UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/10 "2024-07-31T20:24:59Z")

</div>

Yes, we have a src folder in our functions.functions folder and use node to bundle these up into the functions.functions folder which is then deployed to the server. Its pretty tidy. There really should be clearer documention about how to do this in the dev docs, or at least a link to some docs on how to set it up.  
It is not very often we need to do this but have had to for things like stripe payment end points and encrytion.

---

<div class="post-metadata">

**Author:** ![Rahul\_Sharma](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@Rahul\_Sharma](https://community.hubspot.com/u/Rahul_Sharma)\
**Post date:** [August 1, 2024, 1:05pm UTC](https://community.hubspot.com/t/require-package-in-serverless-functions/78247/11 "2024-08-01T13:05:10Z")

</div>

Can you share any reference for it?
