Node.js cannot use import statement outside a module

i am using the below code but i am getting this error

(node:9612) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\a0bra\Desktop\Javascript Projects\Bsc_Run_Bot\test.js:1
import { Moralis } from '@moralis/sdk';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47
import { Moralis } from '@moralis/sdk';
import { EvmChain } from '@moralisweb3/evm-utils';

const chain = EvmChain.BSC;

const address = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';

await Moralis.start({
    apiKey: '..TEvj3CvelU...s',
    // ...and any other configuration
});

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

You have to use require or to set that module in configuration.

You will find the solution in another similar forum thread

Can you send link?

const Moralis = require("moralis/sdk");
      const EvmChain = require("moralisweb3/evm-utils");
      
      const chain = EvmChain.BSC;
      
      const address = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
      
      await Moralis.start({
          apiKey: 'dadasdsadas',
          // ...and any other configuration
      });
      
      const response = await Moralis.EvmApi.token.getTokenPrice({
          address,
          chain,
      });
      console.log(response.result);

C:\Users\a0bra\Desktop\Javascript Projects\Bsc_Run_Bot\app.js:23
const Moralis = require(“moralis/sdk”);
^

SyntaxError: await is only valid in async functions and the top level bodies of modules
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions…js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47

type or paste code here

For this error, you need to put any await code inside an async function and then you can run it.

// for Node.js, use these imports
const Moralis = require('moralis').default;
const { EvmChain } = require('@moralisweb3/evm-utils');

async function run() {
  const chain = EvmChain.BSC;

  const address = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';

  await Moralis.start({
    apiKey: '',
    // ...and any other configuration
  });

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

run();
1 Like