[SOLVED] (Cloud Functions) Accidental import of Moralis types results in obscure error

VS Code (and others) sometimes inserts the following import during an autocomplete:

const { default: Moralis } = require("moralis/types");

It usually results in the following error for me:

ReferenceError: Cannot access 'Moralis' before initialization

I can’t tell you how many times this import has busted all my cloud functions and sent me down a rabbit hole for over an hour, only to “remember” it’s a potential issue.

Not sure what the solution is, but this definitely needs to be mitigated somehow. :exploding_head:

I’ve been told this is related to using v1 of the SDK. We currently have multiple applications that rely on the existing codebase. Hopefully I’ll have some time during the upcoming holiday to look at “upgrading”.

If anyone has any further insight, please do share.

Using Moralis.start with the server URL and app Id should probably fix it.

Which SDK functions are you using in the cloud function?

Maybe you can adapt to v2 SDK. With Moralis v2 SDK, you only need an API key to access the Moralis API endpoints.
Read more here:

All you need to do is watch out for this autocomplete whenever you add any imports. With VS Code at least, this also happens with other libraries.

Well sure. I mean, “be careful” and “don’t let it happen” is basically the solution to 99.9999% of coding problems. But the very tooling we use exists to mitigate “just don’t let it happen” types of issues.

Not sure about the practicality, but as an example, in this specific case a solution could be to split types into a distinct but recognizable namespace or at the very least don’t allow a default: of Moralis when importing types so as to prevent any conflict with the (more often used than not) default Moralis object.

I could also go nuclear and disable autoimports in VS Code. But the fact that there’s a conflict on Moralis-land would, IMO, at least warrant consideration for a “fix” (heavy quotes) on the SDK side of things.