[SOLVED] Moralis SDK Error: [C0009] Modules are started already. This method should be called only one time

hey i am using moralis 2.9.0. now and working on next js project .here is my _app.js. code

import React,{useState,useEffect} from ‘react’
import ‘bootstrap/dist/css/bootstrap.css’
import ‘…/styles/globals.css’;
import { MoralisProvider } from “react-moralis”;
import StatusContext from ‘@/store/status-context’;
import Moralis from “moralis”;
import Layout from “./components/Layout/Layout”;

export default function App({ Component, pageProps }) {
const [account, setAccount] = useState("");
const [currentNetwork, setCurrentNetwork] = useState(“mainnets”);
const testnets = {
chains: {
“matic testnet”: “Mumbai (Matic Testnet)”,
ropsten: “Ropsten Testnet”,
“bsc testnet”: “BSC Testnet”,
“avalanche testnet”: “Avalanche Testnet”,
},
};

const mainnets = {
chains: {
eth: “Ethereum Mainnet”,
bsc: “BSC Mainnet”,
matic: “Polygon (Matic) Mainnet”,
avalanche: “Avalanche Mainnet”,
eth_token: “Ethereum Token”,
btc_token: “BSC Token”,
matic_token: “Polygon (Matic) Token”,
ava_token: “Avalanche Token”,
},
};

const networks = currentNetwork === “testnets” ? testnets : mainnets;

const MORALIS_APP_ID = “Ixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx9”;
const MORALIS_SERVER_URL = “https://xxxxxxxxxxxx.grandmoralis.com:2053/server”;

const [error, setError] = useState({
title: “”,
message: “”,
showErrorBox: false,
});
const [success, setSuccess] = useState({
title: “”,
message: “”,
showSuccessBox: false,
});

useEffect(() => {
console.log(“started”);
Moralis.start({
apiKey:
“GomgxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrjmVP1JQ8”,
});
}, []);

return (
<>

<StatusContext.Provider value={[error, success, setSuccess, setError]}>

<Component {…pageProps} account={account} networks={networks} setAccount={setAccount} />

</StatusContext.Provider>

</>
)
}

getting error

Unhandled Runtime Error

Moralis SDK Error: [C0009] Modules are started already. This method should be called only one time.
on this line
Moralis.start({
apiKey:
“GomgxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxrjmVP1JQ8”,
})

Hi, it tells you that you need to call .start only once in your app. To handle this warning you can use this

  if (!Moralis.Core.isStarted) {
    await Moralis.start({
      apiKey: "your key",
    });
  }

Hey @Iulian
i did this now getting error in my functionality
const response = await MainMoralis.EvmApi.token.getWalletTokenBalances({
address,
chain: evmchainvalue,
});

error:
Moralis SDK API Error: [A0003] apiKey is not set

Hi, did you set your key? also can you please check if you have the latest moralis version?
You can run this command

npm install moralis@latest

yeah i did all that things.
i already used this api key in another react projet which is already running .
should i stop the Moralis in that project?

No, it should work fine in this project too

what is MainMoralis here?

const MainMoralis = require(“moralis”).default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");

here is the mainmoralis

Ok did you start it with MainMoralis.start({ apiKey: “yourKey”}) ?

Here is an example on how you should use it

const Moralis = require("moralis").default;

async function main() {
    if (!Moralis.Core.isStarted) {
        Moralis.start({
            apiKey: "your key",
        });
    }

   //test
    await Moralis.EvmApi.utils
        .endpointWeights()
        .then((r) => console.log(r.raw));
}

main();

Hey @Iulian i did not started the mainmoralis with api key. now it’s working fine. thanks a lot

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.