Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serverless - node.js crypto package is not working

Trying to generate RSA keys with crypto package and deploy it on AWS Lambda, I get an error that crypto package is undefined. Are there easy ways to deploy this package to Lambda without having building docker containers?

Yes, I read that node.js native packages have different binaries on mac (my current os) and linux, so there is an approach to build docker and deploy it, but I found it's not very clear for me, so if this is the only way to do it, maybe there are good resources to read about it also.

Thanks!

like image 741
Horhi Avatar asked Apr 07 '26 03:04

Horhi


1 Answers

You need to import the package as require("crypto"). It is just not defined on the global object.

const handler = () => {
  console.log(crypto); // undefined
  console.log(global.crypto); // undefined
  console.log(require("crypto"); // Bingo! :D
}

If you arrived here because you are bundling a Nodejs lambda with rollup and using a version of uuid 7+, then you need to add

external: ["crypto"]

to your rollup.config.js so that rollup does not attempt to replace the require statement by whatever if finds better.

like image 55
enanone Avatar answered Apr 08 '26 18:04

enanone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!