Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when running Vite with import.meta in vite.config.js #19024

Closed
7 tasks done
fffml1 opened this issue Dec 20, 2024 · 14 comments
Closed
7 tasks done

Error when running Vite with import.meta in vite.config.js #19024

fffml1 opened this issue Dec 20, 2024 · 14 comments
Labels
duplicate This issue or pull request already exists

Comments

@fffml1
Copy link

fffml1 commented Dec 20, 2024

Describe the bug

I encountered an error when running a freshly created Vue 3 project with Vite. The project fails to start with the following errors in vite.config.js:

X [ERROR] Expected identifier but found "import"
    (define name):1:0:
      1 │ import.meta.dirname
        ╵ ~~~~~~
X [ERROR] Expected identifier but found "import"
    (define name):1:0:
      1 │ import.meta.filename
        ╵ ~~~~~~
X [ERROR] Expected identifier but found "import"
    (define name):1:0:
      1 │ import.meta.url
        ╵ ~~~~~~
failed to load config from D:\Code\vue-project\vite.config.js
error when starting dev server:
Error: Build failed with 3 errors:
(define name):1:0: ERROR: Expected identifier but found "import"
(define name):1:0: ERROR: Expected identifier but found "import"
(define name):1:0: ERROR: Expected identifier but found "import"
    at failureErrorWithLog (D:\Code\vue-project\node_modules\esbuild\lib\main.js:1476:15)
    at D:\Code\vue-project\node_modules\esbuild\lib\main.js:945:25
    at runOnEndCallbacks (D:\Code\vue-project\node_modules\esbuild\lib\main.js:1316:45)
    at buildResponseToResult (D:\Code\vue-project\node_modules\esbuild\lib\main.js:943:7)
    at D:\Code\vue-project\node_modules\esbuild\lib\main.js:970:16
    at responseCallbacks.<computed> (D:\Code\vue-project\node_modules\esbuild\lib\main.js:622:9)
    at handleIncomingPacket (D:\Code\vue-project\node_modules\esbuild\lib\main.js:677:12)
    at Socket.readFromStdout (D:\Code\vue-project\node_modules\esbuild\lib\main.js:600:7)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)

Reproduction

none

Steps to reproduce

Run npm init vue@latest to create a new Vue 3 project.
Select default options (Vue 3, no TypeScript, no router, no Pinia, etc.).
Navigate to the project folder and run npm install.
Run npm run dev to start the Vite development server.

System Info

Node.js version: v20.17.0
npm version: 10.8.2

Used Package Manager

npm

Logs

No response

Validations

@hykilpikonna
Copy link

Same issue here when upgrading Svelte project from vite 5 to vite 6

@Qullis
Copy link

Qullis commented Dec 20, 2024

The same thing happens with a new React project.

npm create vite@latest my-react-app -- --template react

@newstart0514
Copy link

newstart0514 commented Dec 20, 2024

Vue3 old projects also have similar issues when building new devices, with Node version 20.17.0

@hykilpikonna
Copy link

hykilpikonna commented Dec 20, 2024

What's strange is that, a new Vite+Svelte+TS project created yesterday on Vite 6.0.3 did not have this issue even when upgraded to Vite 6.0.4, but a new Vite+Svelte+TS 6.0.4 project created today has this issue.

image

Diffing the two including node modules seem to say that node_modules/.vite isn't present in the problematic one

@sameignatovich
Copy link

duplicates #19018

@TheRealShreyash
Copy link

TheRealShreyash commented Dec 20, 2024

The same is happening with me in vite+react but I am using vite 6.0.3

@jeff-fe
Copy link
Contributor

jeff-fe commented Dec 20, 2024

It seems to be caused by the new version of esbuild:0:24.1
问题看样子是上游esbuild 0.24.1版本导致的

@diganta688
Copy link

Same thing happens with a new React project.
npm create vite@latest my-react-app -- --template react

X [ERROR] Expected identifier but found "import"
X [ERROR] Expected identifier but found "import"

(define name):1:0:
  1 │ import.meta.dirname

(define name):1:0:
  1 │ import.meta.dirname
(define name):1:0:
  1 │ import.meta.dirname
    ╵ ~~~~~~
  1 │ import.meta.dirname
    ╵ ~~~~~~

    ╵ ~~~~~~

X [ERROR] Expected identifier but found "import"
X [ERROR] Expected identifier but found "import"

(define name):1:0:
  1 │ import.meta.filename
(define name):1:0:
  1 │ import.meta.filename
  1 │ import.meta.filename
    ╵ ~~~~~~

X [ERROR] Expected identifier but found "import"

(define name):1:0:
  1 │ import.meta.url
    ╵ ~~~~~~

failed to load config from C:\vite\d\vite.config.js
error when starting dev server:
Error: Build failed with 3 errors:
(define name):1:0: ERROR: Expected identifier but found "import"
(define name):1:0: ERROR: Expected identifier but found "import"
(define name):1:0: ERROR: Expected identifier but found "import"
at failureErrorWithLog (C:\vite\d\node_modules\esbuild\lib\main.js:1476:15)
at C:\vite\d\node_modules\esbuild\lib\main.js:945:25
at runOnEndCallbacks (C:\vite\d\node_modules\esbuild\lib\main.js:1316:45)
at buildResponseToResult (C:\vite\d\node_modules\esbuild\lib\main.js:943:7)
at C:\vite\d\node_modules\esbuild\lib\main.js:970:16
at responseCallbacks. (C:\vite\d\node_modules\esbuild\lib\main.js:622:9)
at handleIncomingPacket (C:\vite\d\node_modules\esbuild\lib\main.js:677:12)
at Socket.readFromStdout (C:\vite\d\node_modules\esbuild\lib\main.js:600:7)
at Socket.emit (node:events:519:28)
at addChunk (node:internal/streams/readable:559:12)

@sapphi-red
Copy link
Member

Duplicate of #19018

@sapphi-red sapphi-red marked this as a duplicate of #19018 Dec 20, 2024
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Dec 20, 2024
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Dec 20, 2024
@Entri-aswin
Copy link

The same thing happens with a new React project.

npm create vite@latest my-react-app -- --template react

did you get the solution for that?

@Sujan7Neupane
Copy link

npm i -D esbuild@0.24.0

This worked for me

@breizh24
Copy link

breizh24 commented Dec 20, 2024

To avoid adding esbuild to the devDependencies the solution that worked for me was to add an override in the package.json

{
  "name": "Your app",
  "devDependencies": {
    "vite": "^6.0.4",
  },
  "overrides": {
    "vite": {
      "esbuild": "0.24.0"
    }
  },
}

Then delete the node_modules folder and run npm install

@wangxy-gitub
Copy link

Don‘t use vite 6

@val1984
Copy link
Contributor

val1984 commented Dec 20, 2024

v6.0.5 pins esbuild to v0.24.0 which fixes this issue without having to mess with package.json.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests