Heim  >  Fragen und Antworten  >  Hauptteil

Vite kann die Konfiguration nicht aus vite.config.js laden.

Ich habe durch diese Vorgänge eine neue Vue-Anwendung erstellt (gemäß der Vue-Dokumentation)

  1. npm init vue@latest
  2. npm install

Dann versuche ich zu rennen npm run dev. Und dann geschah das.

Meine Umgebung ist diese

Mein 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"
  }
}

Mein 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))
    }
  }
})

Ich habe eine Weile gesucht, ohne Ergebnisse. Dank im Voraus.

P粉510127741P粉510127741358 Tage vor740

Antworte allen(2)Ich werde antworten

  • P粉998920744

    P粉9989207442023-10-27 09:21:19

    我这里也有同样的问题。看起来输出会针对浏览器执行进行优化,并且浏览器不存在诸如“path、fs 等”之类的模块。这是有道理的,因为它是 Nodejs 本身的一部分。它无法在浏览器中运行。到目前为止,这是我的假设。

    查看各种解决方案以了解我为何做出这些假设。

    https://github.com/vitejs/vite/discussions/6849 https://github.com/vitejs/vite/issues/7821#issuecomment- 1142328698

    https://github.com/marcofugaro/browserslist-to-esbuild https://esbuild.github.io/getting-started/

    鉴于这些信息,我更喜欢一个更简单的解决方案来防止使用 Vite 作为捆绑器构建失败。

    配置 rollupOptions

    我认为最简单的解决方案是定义外部。 https://rollupjs.org/configuration-options/#external

    import { resolve } from 'path';
    import { defineConfig } from 'vite';
    
    export default defineConfig({
        plugins: [],
        build: {
            lib: {
                entry: resolve(__dirname, 'src/index.ts'),
                name: 'myLib',
                fileName: 'myLib',
            },
            rollupOptions: {
                external: [/^node:\w+/], // <-- ignores all 'node:*'
            },
        },
    });
    
    

    Antwort
    0
  • P粉520204081

    P粉5202040812023-10-27 00:37:15

    终于找到解决办法了。 问题是由于 package.json 文件冲突造成的。 Vite 使用了位于项目父目录中的错误 package.json 文件,而不是项目自己的 package.json 文件。就像这样 -

    • ~/package.json(错误的文件)
    • ~/Projects/VueProject/package.json(正确的文件)

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

    感谢这个github问题的回答 package.json:1:0: 错误:文件意外结束

    Antwort
    0
  • StornierenAntwort