[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.