[SOLVED] 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

To solve the error, set the type attribute to module when loading the script in your HTML code. When working with ECMAScript modules and JavaScript module import statements in the browser, you’ll need to explicitly tell the browser that a script is module. To do this, you have to add type=“module” onto any â€čscriptâ€ș tags that point to a JavaScript module. Once you do this you can import that module without issues.

<script type="module" src="./index.js"></script>

If you are working on Node.js or react applications and using import statements instead of require to load the modules, then ensure your package.json has a property “type”: “module” as shown below.

{
  // ...
  "type": "module",
  // ...
}

Moreover, In some cases, you may have to use both import and require statements to load the module properly.

// import { parse } from 'node-html-parser';
parse = require('node-html-parser');

This error "Cannot use import statement outside a module " can happen in different cases depending on whether you’re working with JavaScript on the server-side with Node.js , or client-side in the browser. There are several reasons behind this error, and the solution depends on how you call the module or script tag.

1 Like