Cannot Use Import Statement Outside a Module Node

0
604

Cannot Use Import Statement Outside a Module Node: syntaxerror

Cannot Use Import Statement Outside a Module Node is the easiest topic in computer science, In this article, you will learn easily Cannot Use Import Statement Outside a Module Node. In this tutorial, we are going to learn about Cannot Use Import Statement Outside a Module Node.

When we use an es6 import statement to import one JavaScript file inside another, we might see this following error in our browser console.

Cannot Use Import Statement Outside a Module Node
main.js
import { add } from './math.js';

console.log(add(1, 2));
console
Uncaught SyntaxError: Cannot use import statement outside a module

To correct this mistake, add the type=”module” attribute to our main entry JavaScript file, as shown below.

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

This instructs the browser to handle main.js as a module rather than a script file.

SyntaxError: Cannot use import statement outside a module

LEAVE A REPLY

Please enter your comment!
Please enter your name here