TypeError: Cannot read properties of undefined (reading 'start')

Hey guys !

I found Moralisā€™ API really cool so Iā€™m trying to use it in a project of mine, but whenever I try to initialize Moralis with my appā€™s credentials while using an ES6 import (import Moralis from 'moralis/node';) to add Moralisā€™ JS SDK to my NestJS back-end app, I get this error :
TypeError: Cannot read properties of undefined (reading 'start').

Using the old way of importing node packages (const Moralis = require('moralis/node');) ā€œfixesā€ it, but I canā€™t figure out why it the error I described above happens.

Does anybody know how to fix this issue Iā€™m having ?

that error sounds like Moralis variable is undefined in the first case, assuming that somewhere you call Moralis.start.

Youā€™re right, and thatā€™s what I want to do, but for some reason the imported module seems to be undefined.

Hereā€™s the file where Iā€™m having the issue (main.ts) :

import { NestFactory } from '@nestjs/core';
import * as compression from 'compression';
import { AppModule } from './app.module';
import Moralis from 'moralis/node';

async function bootstrap() {
  try {
    const app = await NestFactory.create(AppModule);

    app.use(compression());
    app.enableCors();

    await Moralis.start({
      serverUrl: process.env.MORALIS_SERVER_URL,
      appId: process.env.MORALIS_APP_ID,
    });

    await app.listen(process.env.BACKEND_PORT);

    const NFTs = await Moralis.Web3API.token.getAllTokenIds({
      address: '0x9a534628b4062e123ce7ee2222ec20b86e16ca8f',
    });

    console.log('šŸ”ŗ', NFTs);
  } catch (error) {
    console.error(error);
  }
}

bootstrap();

Using const Moralis = require(moralis/node) seems to Ā« solve Ā» this issue, but I was wondering if there was a proper way of importing Moralisā€™ SDK with ES6 imports.

This is still a problem. I too am using NestJS as my backend, and all forms of ES6 import causes Moralis to be undefined. I think this is some kind of issue with the way the export is defined.

I agree, I think there is an issue with the typedefs for moralis/node compared to the JS code.

I made sort of a hacky TS import to fix the typing and this worked for me.

import { Moralis as MoralisDef } from 'moralis/types/node';
import * as MoralisImport from 'moralis/node';

const Moralis = MoralisImport as unknown as MoralisDef;

Add the ā€œas unknownā€ part to get rid of an eslint warning ;p

2 Likes

Thanks, will use this workaround.

Is there any real fix planned @cryptokid ?

I expect it to work without issues in version 2, you will be able to test it today

1 Like

hey @Grid - I hope this isnā€™t too late for helping you, but this is the solution that I figured out that works for me loading dynamic data in Moralis inspired by @jacob.muchow and @YosephKS. This took me 2 days to figure out :skull: but just tested it on my production URL and localhost and works!!

+++

Pass in getServerSideProps and run the Moralis.Start function first before the actual functional component loads. I just use address to test, but it should load before anything else, meaning itā€™s working, and then Moralis server is initialized before the site loads :fire:

I believe this works because we initialize the Moralis provider before the site loads and then this allows me to directly call the Moralis db in my functional component/page because that is happening 2nd after the Moralis provider loads thanks to getServerSideProps.

import { Moralis as MoralisDef } from 'moralis/types/node';
import * as MoralisImport from 'moralis/node';

const YourPage = ({ address }) => {
  return (
      <div>{address} + Your code here</div>
  )
}

export async function getServerSideProps(context) {
  // const Moralis = require("react-moralis");
  const Moralis = MoralisImport as unknown as MoralisDef;
  // @ts-ignore
  await Moralis.start({ 
      serverUrl: process.env.NEXT_PUBLIC_MORALIS_SERVER_URL, 
      appId: process.env.NEXT_PUBLIC_MORALIS_APP_ID, 
      masterKey: process.env.NEXT_PUBLIC_MORALIS_MASTER_KEY 
  });

  const address = '0x...';

  return {
    props: { 
        address,
    },
};
}