Skip to content

Dev server corrupts a method named import, causing SyntaxError: Unexpected token '(' #22750

Description

@soberm

Describe the bug

In Vite 8.1.0, the dev server's importAnalysis transform mistakes a method or identifier named import (e.g. async import(keys, values) {} inside a class) for a dynamic import() expression. It injects the query-injection helper into the method's parameter list, producing invalid JavaScript:

- async import(keys, values) {
+ async import(__vite__injectQuery(keys, 'import'), values) {

A function/method parameter list cannot contain a call expression, so the browser throws SyntaxError: Unexpected token '(' when it loads the served module, and the app fails to mount.

This is dev-server onlyvite build (production) is unaffected. import is a legal method name (class { import() {} }, obj.import()), so this is valid application code being corrupted by Vite. It regressed between 8.0.16 (OK) and 8.1.0 (broken).

Reproduction

https://stackblitz.com/edit/vitejs-vite-bztul6vj

Steps to reproduce

  1. Scaffold a vanilla app: npm create vite@latest repro -- --template vanilla
  2. Add src/foo.js and import it from src/main.js.
  3. npm install && npm run dev
  4. Open the app.

The served module (Network tab, or curl http://localhost:5173/src/foo.js) contains the corrupted line:

async import(__vite__injectQuery(keys, 'import'), values) {

Downgrade confirms the regression (same app, same Node, only Vite changed):

Vite Served output Result
8.1.0 async import(__vite__injectQuery(keys, 'import'), values) SyntaxError
8.0.16 async import(keys, values) OK

Likely cause: es-module-lexer was bumped to 2.0.0 in 8.1.0. The dev-server import scanner appears to tokenize a bare import identifier/method as a dynamic-import keyword, and the query-injection rewrite then targets it. The rewrite should only apply to genuine dynamic import(...) call expressions, not to identifiers/methods named import.

System Info

#  Key versions observed:
#   vite        8.1.0   (regressed; last good: 8.0.16)
#   es-module-lexer 2.0.0
#   Node        24.17.0

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug: upstreamBug in a dependency of Vitep3-downstream-blockerBlocking the downstream ecosystem to work properly (priority)regressionThe issue only appears after a new release

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions