require('package') in Serverless functions

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)

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

Hi, @JBrake :waving_hand: Thank you for sharing your question. Let’s see if we can get the conversation going for you — hey @Teun @louischausse @Mike_Eastwood, can you lend a hand here?

Thank you very much! — Jaycee

Thanks @Jaycee_Lewis, I can’t be the first one to have this problem!

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

Yes I’m getting that feeling too @Mike_Eastwood . 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 (bottom) it mentions webpacks, but I need an existing package from npm.

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

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.

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. 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.

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

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.

Can you share any reference for it?