close

[Fixed] “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6

Today We are Going To Solve “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6 in Javascript. Here we will Discuss All Possible Solutions and How this error Occurs So let’s get started with this Article.

How to Fix “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6 Error?

  1. How to Fix “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6 Error?

    To Fix “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6 Error just Add type=”module” . You can solve this error by just adding type=”module” into your package.json. Try this. { // ... "type": "module", // ... }

  2. “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6

    To Fix “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6 Error just replace “import” by “require”. Just replace “import” with “require” to solve this error. Use it like given in below example. // import { parse } from 'node-html-parser'; const parse = require('node-html-parser');

Solution 1 : Add type=”module”

You can solve this error by just adding type=”module” into your package.json. Try this.

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

Solution 2 : replace “import” by “require”

Just replace “import” with “require” to solve this error. Use it like given in below example.

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

Solution 3 : Try this code

Just add a type=”module” to your script just like below to solve your error.

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

Solution 4 : use import prompts from “prompts”

To solve your error first of all use import prompts from “prompts”. And after it use "module": "commonjs" in file tsconfig.json

Conclusion

So these were all possible solutions to this error. I hope your error has been solved by this article. In the comments, tell us which solution worked? If you liked our article, please share it on your social media and comment on your suggestions. Thank you.

Also Read This Solutions

Leave a Comment