题意:Vite 无法从 vite.config.js 加载配置。

问题背景:

I created a new vue app by doing these (according to vue docs)

我通过执行以下操作创建了一个新的 Vue 应用(根据 Vue 文档)。

  1. npm init vue@latest
  2. npm install

Then I try to run npm run dev.Then this happened.

然后我尝试运行 npm run dev。然后发生了这个。

My environments are these        我的环境配置信息

  • OS => Ubuntu
  • Node version => 18.7.0
  • npm version => 8.15.0

My package.json        我的 package.json 文件

{
  "name": "vue-project",
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview --port 4173"
  },
  "dependencies": {
    "vue": "^3.2.37"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^3.0.1",
    "vite": "^3.0.4"
  }
}

My vite.config.js        我的 vite.config.js 文件

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

I have been searching for a while now but no avail.Thanks in advance.

我已经搜索了一段时间,但没有找到解决方法。提前感谢。

问题解决:

Finally I found the solution. The problem was because of the package.json file conflict. Vite is using the wrong package.json file located in the Project's parent directory instead of project's own package.json file.Like this -

最后我找到了解决方案。问题是由于 package.json 文件冲突。Vite 正在使用位于项目父目录中的错误 package.json 文件,而不是项目本身的 package.json 文件。就像这样:

  • ~/package.json ( wrong file )
  • ~/Projects/VueProject/package.json ( correct file )

So delete the wrong file and the problem will be fixed.

所以删除错误的文件,问题就会解决。

Thanks to this github issue answer  感谢这个 GitHub 问题的回答。 package.json:1:0: error: Unexpected end of file

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐