SyntaxError – Cannot use import statement outside a module

Last Updated On Sunday 10th Apr 2022

From the 14.x version of the Node, you can import to import the modules you need. You can do either require or import. Unfortunately, you can’t natively mix them.

Cannot use import statement outside a module.

  • It’s most common issues if you try to use ES6 features in your JavaScript project.

In your package.json file, simply add type:module to fix this issue.

	"version": "1.0.0",
"description": "New Kit",
"type": "module,"
	

uncaught syntaxerror: cannot use import statement outside a module

  • Need to add the type="module" attribute
	<script type="module">
   import abc from './abc.js';
</script>
	
  • The Above tells the browser to treat this main.js file as a module instead of a normal script file.