12.8 C
New York

cannot use import statement outside a module


When you attempt to use the import keyword in a JavaScript file that is not an ES6 module, the error “cannot use import statement outside a module” is returned. A new method of structuring JavaScript code called ES6 modules promotes improved code reuse and modularity.

You must confirm that the JavaScript file containing the import statement is an ES6 module in order to correct this problem. Several methods exist for doing this:

The script> tag, which loads the JavaScript file, can be given the type=”module” property.
The module property in the package.json file can be set to true.
You can start the Node.js interpreter with the -experimental-modules flag set.

The import statement should no longer result in an error once you have confirmed that the JavaScript file is an ES6 module.

Related Post

Latest Post