Vite: Dependency optimization failed
Viteの依存関係最適化が失敗した際のエラー原因と解決策
概要
Viteが依存関係のプリバンドル処理に失敗した際に発生するエラーです。
エラーメッセージ
``` [vite] error while updating dependencies: Error: Build failed with 1 error: node_modules/some-package/index.js:1:0: ERROR: Could not resolve “missing-dep” ```
原因
- 依存関係の不整合: パッケージの依存が欠落
- CommonJSモジュールの問題: ESM変換に失敗
- キャッシュの破損: node_modules/.viteキャッシュの問題
- 循環依存: パッケージ間の循環参照
解決策
1. キャッシュをクリア
```bash rm -rf node_modules/.vite npm run dev ```
2. 依存関係を再インストール
```bash rm -rf node_modules package-lock.json npm install ```
3. 問題のパッケージを除外
```typescript // vite.config.ts export default defineConfig({ optimizeDeps: { exclude: [‘problematic-package’], // または強制的に含める include: [‘some-cjs-package’] } }); ```
4. CommonJS互換性を設定
```typescript // vite.config.ts import { defineConfig } from ‘vite’;
export default defineConfig({ build: { commonjsOptions: { transformMixedEsModules: true, include: [/node_modules/] } } }); ```
5. esbuild設定を調整
```typescript export default defineConfig({ esbuild: { supported: { ’top-level-await’: true } } }); ```
よくある間違い
- キャッシュクリアせずに再起動
- optimizeDeps.includeの設定漏れ
- node_modulesの部分的な削除
関連エラー
関連エラー
Vite の他のエラー
この記事は役に立ちましたか?