Search code examples
reactjsnext.jsethereumweb3jsmoralis

Why is Module not found from Moralis in Next.js


I am currently in the process of developing a dApp for a local business, however the moralis node module seems to have started to get buggy.

I am using Next.js & React with Yarn

i ran yarn add moralis with the error as enter image description here

as a result the local server throws this error​ enter image description here

I have tried uninstalling package and reinstalling, removing yarn.lock and package-lock.

I have tried all the other locations available for the (import Moralis from "Location") spot.


Solution

  • Initialize Moralis at the root level of your React application (e.g., in your index.js or App.js file). This will ensure that Moralis is initialized only once when your app starts:

    // index.js or App.js
    import React from 'react';
    import ReactDOM from 'react-dom';
    import App from './App'; // Your main application component
    import Moralis from 'moralis';
    
    Moralis.start({ apiKey: 'YOUR_API_KEY' });
    
    ReactDOM.render(
      <React.StrictMode>
        <App />
      </React.StrictMode>,
      document.getElementById('root')
    );