Cloud function - require is not a function

My dapp used to work about a day ago. Getting this error:

2021-06-09T17:17:25.447Z - TypeError: require is not a function
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:8:21), <anonymous>:1:17)
    at customUserPlugin (/moralis-server/cloud/main.js:8:21)
    at /moralis-server/lib/cloud-code/plugins/index.js:62:15
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Object.initialize (/moralis-server/lib/cloud-code/plugins/index.js:51:3)

Its failing because of these lines below. Please advise…

const BigNumber = require("bignumber.js");
const Web3 = require("web3");
1 Like

Moralis includes both packages by default, so there is no need for those 2 line codes, instead you should be using web3.utils.

Carlos Z

The require function is no longer available in Cloud Functions. We’ll be adding this feature back in a much better way soon.

Yes as Carlos is saying these particular packages are both available from Web3.

const web3 = Moralis.web3ByChain("0x1"); // mainnet
const BN = web3.utils.BN;

https://docs.moralis.io/moralis-server/cloud-functions#web3

https://web3js.readthedocs.io/en/v1.3.4/web3-utils.html#bn

1 Like

Are there any alternatives to toFixed() with BN?

I have managed to find another way of getting around the toFixed() method. I have moved some of the functionality to the front-end where I can utilise BigNumber.js.

Thanks for the help