[SOLVED] NodeJS serverside Moralis error (window is not defined)

If I imported it in a wrong way then it shouldn’t be work with “await Moralis.start({ serverUrl, appId, masterKey })”. I guess.

there are different ways to import it, you can import the browser version or the server version

const Moralis = require(“moralis/node”);
const moralisServer = async () => {
await Moralis.start({ serverUrl, appId, masterKey });
logger.info(“Moralis connected”);
};

It works perfectly with the read functions like “symbol” (Moralis.Web3API.native.runContractFunction). But doesn’t work with any functions where I use Moralis.executeFunction.

And this code doesn’t work with read functions. and gives that error.
const Moralis = require(“moralis/node”);
const moralisServer = async () => {
await Moralis.start({ moralisSecret });
await Moralis.enableWeb3({
privateKey: process.env.PRIVATE_KEY2,
});
}

Moralis.executeFunction requires that private key

did you try with latest version of Moralis SDK?
I know that Moralis.transfer works in some particular circumstances.

No. I didn’t try with the latest I guess. Please suggest me how to work with that.

you install latest version of the sdk, you can check with Moralis.CoreManager.get("VERSION") what version is running now.

latest version now is 1.7.0

Yes. I am using “js1.7.0” version. :sob:

in this case, for now, you can use web3 or ethers syntax directly to execute a function with private key

Please give me a sample code of it. It will be helpful for me. Thanks in advance.

you could read on this thread:

Still. I couldn’t solve the problem. :sob:

What error(s) do you get? Or is it still the same one?

Still it is same. Moralis SDK connection error. Given the error at the top.

I got everything working fine, using:

await Moralis.start({ serverUrl, appId, masterKey, moralisSecret })

as suggested by Rick, you need to add moralisSecret, but do not remove the other params otherwhise the other functions won’t work anymore !

I wonder why this is not in the doc, and it is really hard to debug from the assert !

2 Likes
await Moralis.start({ serverUrl, appId, masterKey, moralisSecret });
await Moralis.enableWeb3({
  chainId: 0x61,
  privateKey: 'priate_key',
});

This works previously with me but now it is not working. I am getting the same error "window is not defined’. I have changed the chain to BSC testnet and Mumbai testnet (changed chain id too). But still it not working. Please let me know how to work with it now.

This stopped working since Moralis discontinued speedy nodes. You can use web3js instead

Please share resource for that with me. That would be great help.

Here’s an example using web3js with a custom rpc which is a similar way to do in nodejs. Nonetheless, you can check out web3js docs. You can also find some blogs out there that explains such

I guess using web3 instance is complicated to code. Is there any other way to enableWeb3 using Moralis?

You will need to use ethers.js or web3.js directly if you want to use a private key to make transactions - you can’t use enableWeb3 now. For example with ethers.js.