Cannot use import statement outside a module in JavaScript (Solved)

A photo of the International Space Station's module

The error “cannot use import statement outside a module” occurs when you use the import statement outside an ES (ECMAScript) module.

If you’re using Node.js, you need to set Node’s module system to ES modules – by adding type: "module" to your package.json file. However, if you’re getting this error in your web browser, add type="module" to your <script> tag.

Here’s how the error looks like in the Node.js environment:


(node:40660) Warning: To load an ES module, set type: module in the package.json or use the .mjs extension.
(Use  to show where the warning was created)
/var/www/node-test/app.js:1
import { isOdd } from './utils.js'
^^^^^^

SyntaxError: Cannot use import statement outside a module

And in the browser:

Uncaught SyntaxError: Cannot use import statement outside a module error displayed in the browser console.

But what's an ES module? You may ask.

ES modules are the standard format to package JavaScript code for reuse.

How to fix "cannot use import statement outside a module" error

As mentioned earlier, this error can happen in two environments:

  1. Node.js
  2. Web browser

Let's see how we can fix the issue on either of these environments.

Node.js: Node.js supports two module systems for loading modules:

  • The CommonJs Modules (the default module system)
  • ES Modules

If you're getting the error in Node.js, the reason is probably Node.js isn't configured to use the ESM module system. Node's default module system is CommonJS, where you export modules with module.exports and load them via require().

Set Node's module system to ES modules by adding "type": "module" to your package.json file.


{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "type": "module",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

If you don't have a package.json file yet, you can create one by running npm init in your project's directory.


npm init

Npm will ask you to provide information about your package, such as name, version, description, author, etc. This information is optional, though.

The above should fix the issue in your Node.js app.

If you're getting the error in the browser: Most browsers have native support for ECMAScript Modules. However, if you get this error in your browser, you need to add type="module" to your <script> tag:


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

And in case you want to write your code directly in the HTML document:


<script type=module>
   // Your JavaScript code here
</script>

Alright, I think that does it! I hope this quick guide could solve your problem.

Thanks for reading.

Author photo

Hey 👋 I'm a software engineer, an author, and an open-source contributor. I enjoy helping people (including myself) decode the complex side of technology. I share my findings on Twitter: @lavary_

If you read this far, you can tweet to the author to show them you care. Tweet a Thanks

In this article:

Disclaimer: This post might contain affiliate links. I might receive a commission if a purchase is made. However, it doesn’t change the cost you’ll pay.