Cloud function - TypeError: require is not a function

This used to work for me until today…

const crypto = require('crypto');
const logger = Moralis.Cloud.getLogger();

Moralis.Cloud.define("saveSellerDataToFile", async (request) => {
  logger.info("saveSellerDataToFile called...");
  
  const sellerData = request.params.sellerData;
  const dataHash = crypto.createHash('sha256').update(sellerData).digest('hex');
  
  return {
    dataHash
  };
});

But today the function fails with…

TypeError: require is not a function

I’ve also tried import but it also does not work.

How to use the default node.js modules like crypto in my cloud functions?

1 Like

Hey @itheum.

crypto package now available on all cloud functions, just remove this line:

const crypto = require('crypto');

also update/restart the server to apply the updates properly.

Carlos Z

1 Like

Excellent - it worked. Thanks for the quick reply.

1 Like