How to Resolve ”Uncaught Syntaxerror: Cannot Use Import Statement Outside a Module”?


To resolve the "Cannot use import statement outside a module" error for NPM/NodeJS, include "type":"module" in the package.json file:

  "name": "...",
  // ...
  "type": "module",
  // ...

If importing directly from the browser, add type="module" to the HTML script tag

<!-- Error -->
<script src="script.js"></script>

<!-- Fixed -->
<script type="module" src="script.js"></script>

Latest Questions

javascript How to Check if a Javascript Array Includes a Given Value? javascript How to Completely Uninstall Nodejs, and Reinstall From Scratch on Mac OS X? javascript How to Map Values of a Javascript Object to a New Object Using a Function?