executeFunction in node.js and run as a script on server

Hey there,

I’m having trouble running a node.js script in the terminal where I want to executeFunction by private_key.

Here is the code:

const Moralis = require('moralis/node');

var ABI = ....;

const serverUrl = "...";

const appId = "...";

const masterKey = "...";

const rebase = async () => {

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

    await Moralis.enableWeb3({

        chainId: 0xa86a,

        privateKey: "private_key",

    });

    readOptions = {

        contractAddress: contract_address,

        functionName: "rebase",

        abi: ABI

    };

    const message = await Moralis.executeFunction(readOptions);

};
rebase();

and the error I’m getting is below:

if (!((_window = window) !== null && _window !== void 0 && _window.ethereum)) {

Is there to sign with private_key by not using Moralis.enableWeb3 ??

I didn’t test executeFunction, you will also need to set moralisSecret, here is an example for transfer:

Hey, I saw your reply in a couple of threads but that doesn’t solve the issue.
You’re using enableWeb3 also which is throwing this error:
if (!((_window = window) !== null && _window !== void 0 && _window.ethereum)) {

I didn’t have any problem when using enableWeb3, and I used moralisSecret

Where is that coming from? What is the actual error?

Since the script is running in the terminal, the window variable cannot be found and is throwing the error.

Below is the full log:

node_modules\moralis\lib\node\Web3Connector\InjectedWeb3Connector.js:66
    if (!((_window = window) !== null && _window !== void 0 && _window.ethereum)) {
    ^

ReferenceError: window is not defined
    at InjectedWeb3Connector.verifyEthereumBrowser (D:\...\node_modules\moralis\lib\node\Web3Connector\InjectedWeb3Connector.js:66:5)
    at InjectedWeb3Connector.activate (D:\...\\node_modules\moralis\lib\node\Web3Connector\InjectedWeb3Connector.js:72:10)
    at InternalWeb3Provider.activate (D:\...\\node_modules\moralis\lib\node\InternalWeb3Provider.js:65:30)
    at Function.enableWeb3 (D:\...\\node_modules\moralis\lib\node\MoralisWeb3.js:215:45)
    at rebase (D:\...\\index.js:19:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Did you try it on a terminal as a bot/script?

btw masterKey variable above holds the moralisSecret

this is the syntax that you should use:

    await Moralis.start({ moralisSecret: "afsdfasdfsdf" });

what about serverUrl and appId ?

they are not needed for executeFunction

1 Like

Got it, It’s working now that I removed them. Thank you very much for your help.

2 Likes