---
title: "Fix SyntaxError Cannot use import statement outside a module in the browser and in Node.js"
description: "SyntaxError: Cannot use import statement outside a module means the file ran as a classic script or as CommonJS. Load it as a module, and the same import runs."
canonical: "https://logbrew.co/en/errors/cannot-use-import-statement-outside-a-module"
last_updated: "2026-11-21"
---

# Fix SyntaxError Cannot use import statement outside a module in the browser and in Node.js

`SyntaxError: Cannot use import statement outside a module` means the file ran as a classic script or as CommonJS. Load it as a module, and the same `import` runs.

Tested on November 21, 2026 with Chrome 157, Safari 27.2, Node.js 22.18.0, and @logbrew/browser 0.1.22

By [Furkan Erday](https://github.com/furkanerday). Furkan Erday is the founder of LogBrew. He builds LogBrew with Claude Code and Codex every day.

Canonical: https://logbrew.co/en/errors/cannot-use-import-statement-outside-a-module

## What 5 script tags did with 1 `import`

| How the page loaded it | Code ran | Issue in LogBrew |
| --- | --- | --- |
| `<script src>` with no type | No | `SyntaxError` |
| Inline `<script>` | No | `SyntaxError` |
| `<script src>` from another origin | No | `Error`, line 0 |
| SDK in a `type="module"` script | No | No |
| `<script type="module">` | Yes | No error |

Chrome 157 and Safari 27.2 gave the same result for each tag.

## Find where the file ran as a classic script

### Look at the script tag that loads the file

The tag has no `type` attribute, so the browser parses `app.js` as a classic script, and a classic script cannot use `import`. Chrome 157 stopped at line 1, column 1, before any line of the file ran. An inline script gave the same message.

```bash
<!-- index.html -->
<script src="/app.js"></script>

// app.js
import { pour } from "./tap.js";

pour("IPA");
```

Output

```text
Uncaught SyntaxError: Cannot use import statement outside a module
```

### Safari gives a different message for the same file

Safari 27.2 parsed the same `app.js` and threw `SyntaxError: Unexpected token '{'. import call expects one or two arguments.` The message is about an `import()` call and does not say module. The cause and the fix are the same.

Output

```text
SyntaxError: Unexpected token '{'. import call expects one or two arguments.
```

### An export in a classic script fails too

A file that only exports, loaded with a classic tag, stops at its first `export`. Chrome said `Unexpected token 'export'`, and Safari said `Unexpected keyword 'export'`. Both mean that the page loaded a module as a classic script.

```bash
// tap.js, loaded with <script src="/tap.js"></script>
export function pour(beer) {
  document.body.dataset.poured = beer;
}
```

Output

```text
Uncaught SyntaxError: Unexpected token 'export'
```

### In Node.js, read the type field of package.json

Node.js 22.18.0 threw the same message when `package.json` had `"type": "commonjs"`. The stack goes through `wrapSafe` in the CommonJS loader. With no `type` field, Node.js ran the file as a module and printed a warning instead.

```bash
// package.json
{ "name": "lab", "type": "commonjs" }

// node app.js
import { pour } from "./tap.js";
```

Output

```text
import { pour } from "./tap.js";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1620:18)
    at Module._compile (node:internal/modules/cjs/loader:1662:20)
```

## Load the file as a module

### Add the module type to the script tag

With `type="module"` on the same tag, `app.js` and its `import` ran in Chrome 157 and Safari 27.2. The browser then loads `tap.js` for you. If a classic tag also loads `tap.js`, remove that tag, because it still stops at `export`.

```bash
<script type="module" src="/app.js"></script>
```

### Set the module type in package.json

Set `"type": "module"` in `package.json`, or rename the file to `.mjs`. Both ran our file with no warning. Node.js reads a `.mjs` file as a module and a `.cjs` file as CommonJS, whatever the `type` field says.

```bash
// package.json
{ "name": "lab", "type": "module" }

// node app.js
```

Output

```text
poured IPA
```

### Load the LogBrew SDK in a classic script first

A parse error fires before any line of that file runs, so the error listener must already be on the page. The browser runs a module script only after it has parsed the page, so a module script comes too late. With the SDK in a classic script in `<head>`, LogBrew recorded the error. With the SDK in a `type="module"` script, it recorded nothing.

```bash
// logbrew.ts
import { installLogBrewBrowser } from "@logbrew/browser";

installLogBrewBrowser({ clientKey: "LOGBREW_BROWSER_KEY" });

// bun build logbrew.ts --format=iife --outfile=public/logbrew.js
// index.html, in <head> before every other script:
// <script src="/logbrew.js"></script>
```

## What we saw when we tested it

We served 1 page at `http://127.0.0.1:28180` and opened it in Chrome 157 and Safari 27.2, with `@logbrew/browser` 0.1.22 loaded first. We loaded the same code in 5 ways and read `error.message` from the `error` event.

Chrome named the cause, and Safari did not. Both browsers ran the file with `type="module"`. A script from another origin gave only `Script error.`, with no file name and line 0, because the browser hides the details of a script from another origin.

Node.js 22.18.0 gave the error only with `"type": "commonjs"`. With no `type` field, it ran the file and printed the warning below.

Output

```text
poured IPA
(node:37250) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///…/app.js is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /…/package.json.
```

|  | Chrome 157 | Safari 27.2 |
| --- | --- | --- |
| `<script src>` with no type | `Cannot use import statement outside a module` | `Unexpected token '{'. import call expects one or two arguments.` |
| Inline `<script>` with no type | `Cannot use import statement outside a module` | `Unexpected token '{'. import call expects one or two arguments.` |
| `tap.js` with an `export`, classic tag | `Unexpected token 'export'` | `Unexpected keyword 'export'` |
| `<script src>` from another origin | `Script error.` | `Script error.` |
| `<script type="module">` | Ran | Ran |

## Why a classic script cannot use import

MDN's page for this error says that you can use an `import` declaration only at the top level of a module, and that the usual cause is a file that does not run as a module. A browser reads a file as a module only when its tag has `type="module"`. With no type, the browser parses the file as a classic script and stops at the first `import`.

A classic script can use the `import()` function call. Safari's message names that call, so it reports a missing argument and not a missing module.

Node.js decides for each file. It reads the file extension first, then the `type` field of the nearest `package.json`. The Node.js docs say that since v22.7.0 and v20.19.0, a file with no `type` field that does not parse as CommonJS runs as a module.

nodejs/node#39353 asked in 2021 for Node.js to choose by the entry point and not by `package.json`. It was closed in 2022. Setting `type` or using `.mjs` still makes the choice clear, and it removes the warning.

1. `.mjs` `.cjs`
2. `package.json` `type`
3. Syntax detection, when there is no `type` field

- [nodejs/node#39353](https://github.com/nodejs/node/issues/39353) Detect require vs. import based on the entry point, not package.json. Closed, 45 comments.

## What LogBrew records

The SDK recorded each parse error as 1 issue named `Browser error: SyntaxError`, with the grouping key `browser.error:SyntaxError`. The issue had the file path and line, no stack frames, and the message `redacted`, because the SDK does not send error messages.

The import and export errors from both browsers went into that 1 issue, because the key holds only the error type. The `sourcePath` tells you which file to fix. The script from another origin became a separate issue, `Browser error: Error`, with line 0 and no file path.

With the SDK in a module script, LogBrew recorded nothing for the same page. Load the SDK in a classic script before your code, as in the last step.

- `Browser error: SyntaxError`
  - `mechanism`: `browser.error`
  - `handled`: `false`
  - `sourcePath`: `/app.js`
  - `lineNumber`: `1`
  - Stack frames: 0
  - Grouping key: `browser.error:SyntaxError`
  - Message: `redacted`
- `Browser error: Error`
  - `lineNumber`: `0`
  - Grouping key: `browser.error:Error`

## Questions people ask

### What does Cannot use import statement outside a module mean?

The file has an `import` declaration, but it ran as a classic script in the browser or as CommonJS in Node.js. Load it as a module to fix it.

### How do I fix it in the browser?

Add `type="module"` to the script tag that loads the file. In our test the same `app.js` then ran in Chrome 157 and Safari 27.2.

### How do I fix it in Node.js?

Set `"type": "module"` in `package.json`, or rename the file to `.mjs`. Since v22.7.0, Node.js also detects module syntax in a package with no `type` field and prints a warning.

### Why does Safari show a different error?

Safari 27.2 reported `Unexpected token '{'. import call expects one or two arguments.` for the same file. It is the same problem, with the same fix.

### What does Unexpected token export mean?

A classic script cannot use `export` either. Chrome said `Unexpected token 'export'`, and Safari said `Unexpected keyword 'export'`. Load that file as a module too.

## See every script that fails to parse

The LogBrew browser SDK records each parse error with its file and line, grouped by error type. Start on the generous free tier, no card needed.

[Start Free](https://logbrew.co/en)

## Sources

- [MDN, SyntaxError: import declarations may only appear at top level of a module](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/import_decl_module_top_level)
- [MDN, Using modules on the web](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules/Modules_on_the_web)
- [Node.js docs, Determining module system](https://nodejs.org/api/packages.html#determining-module-system)
- [nodejs/node#39353](https://github.com/nodejs/node/issues/39353)

## More error fixes

- [Fix the script error that hides its message, file, and line](https://logbrew.co/en/errors/script-error)
- [Fix the Failed to fetch error and tell its 3 causes apart](https://logbrew.co/en/errors/failed-to-fetch)
- [Fix java.lang.NullPointerException in Kotlin and Android from its message](https://logbrew.co/en/errors/nullpointerexception)
- [Fix Minified React error #418 and #185 from the full message and your source line](https://logbrew.co/en/errors/minified-react-error)
- [Fix AxiosError Network Error and find which of its 3 causes you have](https://logbrew.co/en/errors/axios-network-error)
- [Fix the HTTP 499 error and find out who stopped waiting](https://logbrew.co/en/errors/http-499)
- [JavaScript errors and JVM exceptions we reproduced, and how to fix each one](https://logbrew.co/en/errors)
