# Fix 'cannot use import statement outside a module'

> Learn how to fix the cannot use import statement outside a module error by adding type module to package.json in Node.js, or to your script tag in browsers.

Author: Flavio Copes | Published: 2022-04-19 | Canonical: https://flaviocopes.com/fix-cannot-use-import-outside-module/

I stumbled on this error: **Uncaught SyntaxError: cannot use import statement outside a module** while importing a function from a [JavaScript](https://flaviocopes.com/javascript/) file.

This error occurs for one reason: you're trying to use `import` and you're not inside an [**ES module**](https://flaviocopes.com/es-modules/).

It can happen in a [Node.js](https://flaviocopes.com/nodejs/) environment, or in the browser.

First, here's the solution for Node.js: I had to add a `package.json` file in the folder of the project and add:

```json
{
  "type": "module"
}
```

In the browser instead, you have to add the `type` attribute with the value `module` when you load the script, like this:

```html
<script type="module" src="./file.js"></script>
```

instead of 

```html
<script src="./file.js"></script>
```
