MoralisWeb3 / Moralis-JS-SDK

Moralis Official Javascript SDK
https://docs.moralis.io
Other
368 stars 257 forks source link

NodeJS Typescript Moralis is undefined #248

Closed dbvcode closed 2 years ago

dbvcode commented 2 years ago

New Bug Report

Checklist

Issue Description

The import of Moralis in a NodeJS Typescript project does not work as expected. There seems to be a object / types error. In the following scenarios: A: The object is there and does call the Moralis functions, but there is no Types assistance in the code. B: I have types assistance when I write the code but when I compile the object is undefined. C: A dirty hack that seems to work for now, but not sure if it's the right thing

Steps + code to reproduce

A:

// eslint-disable-next-line @typescript-eslint/no-var-requires
const Moralis = require('moralis/node');
console.log(Moralis);

B:

import Moralis from 'moralis/node';
console.log(Moralis);

C:

import * as Moralis_ from 'moralis/node';
import { Moralis as MoralisTypesHack } from 'moralis';
const Moralis: MoralisTypesHack = Moralis_ as any;

Actual Outcome

A: logs the the Moralis object but does not assist in the code

B: logs undefined but provides assistance when writing code

C: logs Moralis object and also assists with the code

Expected Outcome

I would expectimport Moralis from 'moralis/node'; or import {Moralis} from 'moralis/node'; to work out of the box.

Environment

An NX workspace with a bunch of apps including the nodeJS one I'm playing with "@angular/animations": "13.1.1", "@angular/common": "13.1.1", "@angular/compiler": "13.1.1", "@angular/core": "13.1.1", "@angular/forms": "13.1.1", "@angular/platform-browser": "13.1.1", "@angular/platform-browser-dynamic": "13.1.1", "@angular/router": "13.1.1", "@capacitor/core": "^3.3.4", "@feathers-plus/cache": "^1.4.0", "@feathersjs/authentication": "^4.5.12", "@feathersjs/authentication-client": "^4.5.13", "@feathersjs/authentication-local": "^4.5.12", "@feathersjs/authentication-oauth": "^4.5.12", "@feathersjs/configuration": "^4.5.12", "@feathersjs/errors": "^4.5.12", "@feathersjs/express": "^4.5.12", "@feathersjs/feathers": "^4.5.12", "@feathersjs/socketio": "^4.5.13", "@feathersjs/socketio-client": "^4.5.13", "@feathersjs/transport-commons": "^4.5.12", "@ionic/angular": "^5.8.3", "@nrwl/angular": "13.4.3", "@pm2/io": "^5.0.0", "bignumber.js": "^9.0.2", "bson": "^4.6.1", "compression": "^1.7.4", "cors": "^2.8.5", "echarts": "^5.2.2", "ethers": "^5.5.3", "feathers-hooks-common": "^5.0.6", "feathers-memory": "^4.1.0", "feathers-mongodb": "^6.4.1", "feathers-mongoose": "^8.5.1", "feathers-profiler": "^0.1.5", "feathers-reactive": "^0.8.2", "feathers-shallow-populate": "^2.5.1", "helmet": "^4.6.0", "immer": "^9.0.7", "lodash": "^4.17.21", "luxon": "^2.3.0", "matomo-tracker": "^2.2.4", "mongodb": "^3.7.3", "mongodb-core": "^3.2.7", "mongoose": "^6.1.6", "moralis": "^1.2.4", "nedb": "^1.8.0", "ngx-echarts": "^8.0.1", "node-schedule": "^2.1.0", "path": "^0.12.7", "puppeteer": "^11.0.0", "rss-parser": "^3.12.0", "rxjs": "~6.6.0", "serialize-error": "^8.1.0", "tslib": "^2.0.0", "winston": "^3.4.0", "winston-daily-rotate-file": "^4.5.5", "zone.js": "0.11.4"

Server

Client

Logs

ErnoW commented 2 years ago

It might be related to your setup. I just tried a minimal nodeJs app with express and it seems to work correctly

package.json:

{
  "name": "moralis-node-app",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "devDependencies": {
    "@types/express": "^4.17.13",
    "@types/node": "^16.9.1",
    "ts-node-dev": "^1.1.8",
    "typescript": "^4.4.3"
  },
  "dependencies": {
    "express": "^4.17.1",
    "moralis": "^1.2.7"
  },
  "scripts": {
    "dev": "ts-node-dev ./src/index.ts",
    "start": "tsc && node ./lib/index.js"
  }
}

tsconfig:

{
  "compilerOptions": {
    "target": "es5" ,
    "module": "commonjs" ,
    "rootDir": "./src" ,
    "baseUrl": "./src" ,
    "outDir": "./lib",
    "esModuleInterop": true ,
    "forceConsistentCasingInFileNames": true ,
    "strict": true ,
    "useUnknownInCatchVariables": false ,
    "skipLibCheck": true
  },
  "include": ["src"]
}

./src/index.ts

import Moralis from "moralis/node";

Moralis.start({
  appId,
  serverUrl,
});

This gives complete Typescript types

dbvcode commented 2 years ago

That's interesting. In my case the types are there, but when I launch I have the problem. If you do a console.log(Moralis) do you have the Moralis object? As I have stated above, mine is undefined. I don't have the esModuleInterop option.

ErnoW commented 2 years ago

Yes correct, console.log(Moralis) returns the Moralis instance, and the code is type-checked. Please check if you can get it to work in a minimal app, without other dependencies and config, like I did above to confirm if it works.

ErnoW commented 2 years ago

Closing this issue as it is not a bug and likely because of config/environment/dependencies. If you need support, you can reach out on forum.moralis.io