[SOLVED] Moralis.EvmApi.token.getTokenPrice

Hello everyone.

I installed moralis:

npm i moralis
npm i @moralisweb3/core @moralisweb3/evm @moralisweb3/evm-api @moralisweb3/evm-wallet-connect-connector @moralisweb3/evm-utils

I use:

node v14.18.0
npm v8.19.2

I use this code:

import Moralis  from 'moralis';
import { EvmChain } from '@moralisweb3/evm-utils';

const chain = EvmChain.ETHEREUM;
const address = '0x1234567890123456789012345678901234567890';

await Moralis.start(
{
    apiKey: '...'
});

const response = await Moralis.EvmApi.token.getTokenPrice(
{
    address,
    chain,
});

console.log(' ');
console.log('EvmChain');
console.log(EvmChain);
console.log('response.result');
console.log(response.result);

and got this error:

/../node_modules/@moralisweb3/core/lib/MoralisCoreProvider.js:10
	throw new Error_1.MoralisCoreError({
		  ^

MoralisError [Moralis SDK Core Error]: [C0008] Default instance of MoralisCore is not set
    at Function.MoralisCoreProvider.getDefault (/../node_modules/@moralisweb3/core/lib/MoralisCoreProvider.js:10:19)
    at Function.EvmChain.create (/../node_modules/@moralisweb3/evm-utils/lib/dataTypes/EvmChain/EvmChain.js:211:52)
    at Function.get (/../node_modules/@moralisweb3/evm-utils/lib/dataTypes/EvmChain/EvmChain.js:27:29)
    at file:///../dev/moralis/EvmApi.js:4:24
    at ModuleJob.run (internal/modules/esm/module_job.js:183:25)
    at async Loader.import (internal/modules/esm/loader.js:178:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5) {
  isMoralisError: true,
  code: 'C0008',
  details: undefined
}

How I can fix it?

Thanks.

Can you post the full code that you’re using? Are you using a framework e.g. Express or Next.js?

Which versions of moralis, etc. are you using? These are the latest, can you try updating to these if you are not using them:

"@moralisweb3/core": "^2.6.7",
"@moralisweb3/evm-api": "^2.6.7",
"@moralisweb3/evm-utils": "^2.6.7",
"moralis": "^2.6.7"
1 Like

It’s already posted above.

No. I use nodejs as also posted above.

From package.json:

"@moralisweb3/core": "^2.6.7",
"@moralisweb3/evm": "^2.0.0-alpha.2",
"@moralisweb3/evm-api": "^2.6.7",
"@moralisweb3/evm-utils": "^2.6.7",
"@moralisweb3/evm-wallet-connect-connector": "^2.0.0-alpha.2"

Needed to make sure of your code and environment as your address isn’t valid and use of ESM. You will need to do something like:

import Moralis from 'moralis';
const Moralis2 = Moralis.default;
import { EvmChain } from '@moralisweb3/evm-utils';

const chain = EvmChain.ETHEREUM;
const address = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';

await Moralis2.start({
  apiKey: 'test',
});

const response = await Moralis2.EvmApi.token.getTokenPrice({
  address,
  chain,
});

console.log('response.result');
console.log(response.result);

Working on my end with versions:

"moralis": "^2.6.7",
"@moralisweb3/evm-utils": "^2.6.7",
1 Like

Now it works! )

So I need always do:

import Moralis from 'moralis';
const Moralis2 = Moralis.default;

?

Thanks.

This seems to be the workaround when using ESM syntax in a strict Node.js environment, otherwise you would just use:

const Moralis = require('moralis').default;
const { EvmChain } = require('@moralisweb3/evm-utils');

// etc.

For something like Next.js, it doesn’t require it.

1 Like

Ouch. Now I see. Thanks.