Skip to content
nkNilay Kabariya

Entry No.001·Fixes··1 min read

Fix: “Cannot use import statement outside a module”

The fix for Node and the browser. On Node 24 a plain .js file now often just works, so here's exactly when the error still happens. Reproduced in both.

by Nilay#node#esm#javascriptFIXED

The error, verbatim

Jump to the fix ↓
SyntaxError: Cannot use import statement outside a module

Uncaught SyntaxError: Cannot use import statement outside a module

Tested on

Node
24.14.0
Browser
Chrome 152
OS
Windows 11 Pro
Contents
  1. The fix01
  2. When does it still happen on Node 24?02
  3. Why it happens03
  4. What didn’t work04

The fix

The file uses import, but whatever runs it treats it as a classic script or CommonJS. Tell it the file is a module.

In the browser

Add type="module" to the script tag:

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

Module scripts don’t load from file:// pages. Open the page through a local server (for example npx vite or npx serve) instead of double-clicking the HTML file.

In Node

Use one of these:

// package.json
{ "type": "module" }

or rename the file to .mjs.

When does it still happen on Node 24?

Node 24 detects import syntax on its own, so the error is rarer than older answers suggest. From the reproductions:

File package.json Result on Node 24
app.js no "type" field Runs, with a MODULE_TYPELESS_PACKAGE_JSON warning
app.js "type": "commonjs" SyntaxError
app.cjs anything SyntaxError
app.js "type": "module" Runs
app.mjs anything Runs

So on current Node you’ll usually see it when a project is explicitly CommonJS, or when a tool runs your code as CommonJS. Older test and TypeScript setups often do that.

The warning in the first row goes away when you add "type": "module". That also skips a second parse, which Node says costs performance.

Why it happens

JavaScript has two loading modes. import and export only exist in ES modules. Browsers treat <script> without type="module" as a classic script. Node treats a file as CommonJS when package.json says so, when the file is .cjs, or on older Node, by default.

What didn’t work

— N.K., end of entry No.001

Useful? Pass it on:Post on X

Related entries

  1. No.002

    Fix: __dirname is not defined in ES module scope (Node 24)

    ES modules don't have __dirname or __filename. The one-line modern fix (import.meta.dirname) and the fallback for older Node, both run on Node 24.

    > console.log(path.join(__dirname, "data.json"));

    FIXED1 min
  2. No.005

    Fix: ERR_REQUIRE_ESM require() of ES Module not supported

    Why require() of an ES module fails, why it mostly stopped failing on Node 22.12+, and the new errors you hit instead. All reproduced on Node 24.

    > Error [ERR_REQUIRE_ESM]: require() of ES Module ...\node_modules\chalk\source\index.js

    FIXED1 min
  3. No.010

    Fix: Vite “Failed to resolve import” for @ alias imports

    Vite can't find an @/ import until the alias is in vite.config. Why tsconfig paths alone don't help, and the new Vite 8 Rolldown wording. Reproduced.

    > [vite] Internal server error: Failed to resolve import "@/utils/hello"

    FIXED1 min

Post card · Newsletter

Get the next fix in your inbox.

One short email when a new entry is published. No spam, never shared, and you can leave any time.

— Nilay

or follow by RSS

By subscribing you agree to the privacy note. One click to leave.

tip: paste the exact error text