できない.dev

Vite を 6 から 7 に上げると動かなくなること

Vite を 6 から 7 に上げると、Node.js 18 では dev サーバーが crypto.hash のエラーで起動せず、Sass の legacy API・splitVendorChunkPlugin・build.target の 'modules' も使えなくなる。
6 と 7 で打ち比べた 8 つの変更点と直し方をまとめる。

公開:

vite@6 を入れたプロジェクトと、同じファイルのまま vite@7 に上げたプロジェクトを並べ、同じコマンドを打ち比べた。
各節の出力は「v6 では」「v7 に上げると」「v7 で直すと」の 3 段で並ぶ。
Node.js の版を変える 2 つの節は、nodejs.org の公式のバイナリ(18.20.8・20.18.3・20.19.0)に PATH を切り替えて打った。

7 の中心の変更は、Node.js 18 の対応を外し、20.19 以上か 22.12 以上を求めるようになったことである。
公式の発表によると、これらの版の Node.js はフラグなしで require(esm) を使えるので、7 は ES Module だけで配られるようになった。
build の既定のブラウザターゲットも上がった。
ほかは、6 以前から非推奨だった機能の削除が中心である。

8 つのうち、install・build・dev サーバーの起動がエラーで止まるのは、Node.js 18・CommonJS 版・peerDependencies・Sass・splitVendorChunkPlugin・transformIndexHtml の 6 つである。
CommonJS 版は "type" が "commonjs" のときに止まり、Sass は legacy API にしか無いオプションを書いていたときに止まる。
ブラウザターゲットの変更は、'modules' と書いていれば build が止まり、書いていなければ出力の中身だけが変わる。
ミドルウェアの順序の変更では、dev サーバーがリクエストを断るか、レスポンスのヘッダーが増える。

公式の発表によると、Vitest と組み合わせるなら Vitest 3.2 以上が要る(このページでは打っていない)。
移行ガイドの「Advanced」のうち、次の項目も打っていない。
optimizeDeps.entries に書いた文字列はいつも glob として扱われるようになった。
モジュールランナーの実行時のコンテキストには __vite_ssr_exportName__ が要るようになった。
何もしていなかった legacy.proxySsrExternalModules と、使われていない型やプロパティも消えた。
最初から非推奨だった Environment API のプロパティ(vitejs/vite#19986)と、HMRBroadcaster などの HotBroadcaster に関わる型(vitejs/vite#19987)も消えた。

7.0.0 の CHANGELOG にある次の削除も打っていない。
experimental.skipSsrTransform のオプションが無くなった(vitejs/vite#20038)。
server.hot の型だった HotBroadcaster と、その addChannel メソッド・channels プロパティも無くなった(vitejs/vite#19988)。
この PR の説明によると、server.hot そのものは残っている。

Node.js 18 では dev サーバーが起動しなくなった

公式の移行ガイドによると、7 は Node.js 18 をサポートせず、20.19 以上か 22.12 以上を求める。
ここでは Node.js 18.20.8 と、それに同梱の npm 10.8.2 で、6 のプロジェクトを 7 に上げた。

v6 では

$ node --version
v18.20.8
$ npm ls vite
node18@1.0.0 /work/node18
`-- vite@6.4.6
$ npx vite > vite.log 2>&1 &
$ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:5173/
200
$ cat vite.log
  VITE v6.4.6  ready in 101 ms
 
  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose

v7 に上げると

$ npm install -D vite@7
npm warn EBADENGINE Unsupported engine {
npm warn EBADENGINE   package: 'vite@7.3.8',
npm warn EBADENGINE   required: { node: '^20.19.0 || >=22.12.0' },
npm warn EBADENGINE   current: { node: 'v18.20.8', npm: '10.8.2' }
npm warn EBADENGINE }
 
added 1 package, and changed 3 packages in 988ms
$ echo $?
0
$ npx vite build
You are using Node.js 18.20.8. Vite requires Node.js version 20.19+ or 22.12+. Please upgrade your Node.js version.
vite v7.3.8 building client environment for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                0.15 kB │ gzip: 0.14 kB
dist/assets/index-Oi0r7DEU.js  0.74 kB │ gzip: 0.42 kB
✓ built in 48ms
$ echo $?
0
$ npx vite > vite.log 2>&1 &
$ cat vite.log
You are using Node.js 18.20.8. Vite requires Node.js version 20.19+ or 22.12+. Please upgrade your Node.js version.
error when starting dev server:
TypeError: crypto.hash is not a function
    at getHash (file:///work/node18/node_modules/vite/dist/node/chunks/config.js:2444:19)
    at getLockfileHash (file:///work/node18/node_modules/vite/dist/node/chunks/config.js:32635:9)
    at getDepHash (file:///work/node18/node_modules/vite/dist/node/chunks/config.js:32638:23)
    at initDepsOptimizerMetadata (file:///work/node18/node_modules/vite/dist/node/chunks/config.js:32097:53)
    at createDepsOptimizer (file:///work/node18/node_modules/vite/dist/node/chunks/config.js:34283:17)
    at new DevEnvironment (file:///work/node18/node_modules/vite/dist/node/chunks/config.js:35063:109)
    at Object.defaultCreateClientDevEnvironment [as createEnvironment] (file:///work/node18/node_modules/vite/dist/node/chunks/config.js:35475:9)
    at file:///work/node18/node_modules/vite/dist/node/chunks/config.js:25640:52
    at Array.map (<anonymous>)
    at _createServer (file:///work/node18/node_modules/vite/dist/node/chunks/config.js:25639:58)

v7 で直すと

$ node --version
v20.19.0
$ npx vite > vite.log 2>&1 &
$ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:5173/
200
$ cat vite.log
1:14:10 AM [vite] (client) Re-optimizing dependencies because lockfile has changed
 
  VITE v7.3.8  ready in 120 ms
 
  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose

npm install -D vite@7 は EBADENGINE の警告を出したが、終了コード 0 で終わった。
このプロジェクトでは npx vite build も、Vite 自身の版の警告を出して通った。
止まったのは dev サーバーで、起動の途中で TypeError: crypto.hash is not a function になった。

Node.js 18 の crypto には hash 関数が無い。
スタックトレースによると、依存の事前バンドルのためにロックファイルのハッシュを取るところで止まっている。

このプロジェクトのように build が通る場合、build だけを回していると気付かない。

Node.js を 20.19 以上か 22.12 以上に上げる。
20.19.0 に替えると、版の警告も消えて dev サーバーが起動した。
最初の起動で出た Re-optimizing dependencies because lockfile has changed は、npm install でロックファイルが変わったので依存の事前バンドルをやり直した、という知らせである。

CommonJS 版が無くなり、"type" が "commonjs" だと Node.js 20.18 で設定ファイルを読めない

6 は Node.js から使う API を、CommonJS 版と ES Module 版の 2 つで配っていた。
7 は ES Module 版だけになった。
公式の発表によると、20.19 以上を求めるのは、CommonJS から ES Module を require で読む require(esm) がフラグなしで使える版だからである。
ここでは package.json の "type" を "commonjs" にしたプロジェクトで、import で書いた vite.config.js を Node.js 20.18.3 で読ませた。

v6 では

$ node --version
v20.18.3
$ npm pkg get type
"commonjs"
$ cat vite.config.js
import { defineConfig } from "vite";
 
export default defineConfig({});
$ npx vite build
The CJS build of Vite's Node API is deprecated. See https://vite.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.
vite v6.4.6 building for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                0.15 kB │ gzip: 0.14 kB
dist/assets/index-Oi0r7DEU.js  0.74 kB │ gzip: 0.42 kB
✓ built in 47ms
$ echo $?
0

v7 に上げると

$ npm install -D vite@7
npm warn EBADENGINE Unsupported engine {
npm warn EBADENGINE   package: 'vite@7.3.8',
npm warn EBADENGINE   required: { node: '^20.19.0 || >=22.12.0' },
npm warn EBADENGINE   current: { node: 'v20.18.3', npm: '10.8.2' }
npm warn EBADENGINE }
 
added 1 package, and changed 3 packages in 776ms
$ echo $?
0
$ npx vite build
You are using Node.js 20.18.3. Vite requires Node.js version 20.19+ or 22.12+. Please upgrade your Node.js version.
failed to load config from /work/cjs/vite.config.js
error during build:
Error [ERR_REQUIRE_ESM]: require() of ES Module /work/cjs/node_modules/vite/dist/node/index.js from /work/cjs/vite.config.js not supported.
Instead change the require of index.js in /work/cjs/vite.config.js to a dynamic import() which is available in all CommonJS modules.
    at _require.extensions.<computed> [as .js] (file:///work/cjs/node_modules/vite/dist/node/chunks/config.js:36177:9)
    at Object.<anonymous> (/work/cjs/vite.config.js:25:19)
    at _require.extensions.<computed> [as .js] (file:///work/cjs/node_modules/vite/dist/node/chunks/config.js:36176:44)
$ echo $?
1

v7 で直すと

$ node --version
v20.19.0
$ npx vite build
vite v7.3.8 building client environment for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                0.15 kB │ gzip: 0.14 kB
dist/assets/index-Oi0r7DEU.js  0.74 kB │ gzip: 0.42 kB
✓ built in 43ms
$ echo $?
0

6 の build は、CommonJS 版の API は非推奨だという警告を出して通った。
7 は vite.config.js を読むところで ERR_REQUIRE_ESM になった。
エラー文によると、vite.config.js から require() で vite の ES Module 版を読もうとして断られている。
import で書いた設定ファイルでも、"type" が "commonjs" のこのプロジェクトでは require で読まれた。
npm install は EBADENGINE の警告だけで通るので、build を打つまで気付かない。

Node.js を 20.19 以上か 22.12 以上に上げる。
20.19.0 に替えると、設定ファイルを書き換えずに build が通り、CommonJS 版の警告も出なくなった。
Node.js を上げずに、設定ファイルの名前や "type" の側で避けられるかは、このページでは打っていない。
7 は Node.js 20.18 を対象から外しているので、Node.js を上げるほうが先である。

@types/node 18 が入っていると npm で vite@7 を入れられない

7 は peerDependencies に書く版の範囲を狭めた。
@types/node は Node.js の対応に合わせて ^20.19.0 || >=22.12.0 になり、@types/node@18 は範囲から外れた。
@types/node は任意の peerDependencies(エラー文の peerOptional)だが、入っている版が範囲から外れていると npm が止める。
範囲の ^20.19.0 からは、20.18 以前の 20 系も外れる(このページで打ったのは @types/node@18 だけである)。

v6 では

$ npm ls vite @types/node
peer@1.0.0 /work/peer
+-- @types/node@18.19.130
`-- vite@6.4.6
  `-- @types/node@18.19.130 deduped
$ npm view vite@6.4.6 peerDependencies
{
  tsx: '^4.8.1',
  jiti: '>=1.21.0',
  less: '*',
  sass: '*',
  yaml: '^2.4.2',
  stylus: '*',
  terser: '^5.16.0',
  sugarss: '*',
  '@types/node': '^18.0.0 || ^20.0.0 || >=22.0.0',
  lightningcss: '^1.21.0',
  'sass-embedded': '*'
}

v7 に上げると

$ npm install -D vite@7
npm error code ERESOLVE
npm error ERESOLVE could not resolve
npm error
npm error While resolving: vite@7.3.8
npm error Found: @types/node@18.19.130
npm error node_modules/@types/node
npm error   dev @types/node@"^18.19.130" from the root project
npm error
npm error Could not resolve dependency:
npm error peerOptional @types/node@"^20.19.0 || >=22.12.0" from vite@7.3.8
npm error node_modules/vite
npm error   dev vite@"7" from the root project
npm error
npm error Conflicting peer dependency: @types/node@26.6.6
npm error node_modules/@types/node
npm error   peerOptional @types/node@"^20.19.0 || >=22.12.0" from vite@7.3.8
npm error   node_modules/vite
npm error     dev vite@"7" from the root project
npm error
npm error Fix the upstream dependency conflict, or retry this command with --force or --legacy-peer-deps to accept an incorrect (and potentially broken) dependency resolution.
npm error
npm error
npm error For a full report see:
npm error /root/.npm/_logs/2026-11-13T01_14_16_074Z-eresolve-report.txt
npm error A complete log of this run can be found in: /root/.npm/_logs/2026-11-13T01_14_16_074Z-debug-0.log
$ echo $?
1
$ npm view vite@7.3.8 peerDependencies
{
  tsx: '^4.8.1',
  jiti: '>=1.21.0',
  less: '^4.0.0',
  sass: '^1.70.0',
  yaml: '^2.4.2',
  stylus: '>=0.54.8',
  terser: '^5.16.0',
  sugarss: '^5.0.0',
  '@types/node': '^20.19.0 || >=22.12.0',
  lightningcss: '^1.21.0',
  'sass-embedded': '^1.70.0'
}

v7 で直すと

$ npm install -D vite@7 @types/node@24
changed 5 packages in 823ms
npm warn install-scripts 1 package has install scripts not yet covered by allowScripts:
npm warn install-scripts   esbuild@0.28.2 (postinstall: node install.js)
npm warn install-scripts
npm warn install-scripts Run `npm install-scripts ls` to review, or `npm install-scripts approve <pkg>` to allow.
$ echo $?
0
$ npm ls vite @types/node
peer@1.0.0 /work/peer
+-- @types/node@24.19.2
`-- vite@7.3.8
  `-- @types/node@24.19.2 deduped

npm install -D vite@7 は ERESOLVE で止まり、終了コードは 1 になった。
npm view で比べると、sass・less・stylus・sugarss・sass-embedded も "*" から下限つきの範囲に変わっている。
たとえば sass は ^1.70.0 になった。
移行ガイドの「Advanced」は、この変更を CSS プリプロセッサの版の範囲として書いている。
@types/node の範囲も同じ 7 で変わったことは、npm view の出力で分かる。

@types/node を、使っている Node.js の版に合わせて上げる。
このページの Node.js は 24 なので、@types/node@24 を vite@7 と一緒に入れると install が通った。
打ったのは npm 11.19.1 だけで、pnpm と yarn での挙動は打っていない。
1.70 より前の sass が入ったプロジェクトも、このページでは打っていない。

既定のブラウザターゲットが Safari 16 などに上がり、'modules' が無くなった

build.target を書かないときの既定が、'modules' から 'baseline-widely-available' に替わった。
公式の移行ガイドによると、Chrome は 87 から 107、Edge は 88 から 107、Firefox は 78 から 104、Safari は 14.0 から 16.0 に上がった。
2025-05-01 の時点で Baseline Widely Available に入った機能に合わせたものである。
6 の既定の名前だった 'modules' は、7 では使えない。
ここでは private メソッドと #count in obj を使う class を build した。

v6 では

$ cat src/main.js
class Counter {
  #count = 0;
  #increment() {
    this.#count++;
  }
  static isCounter(obj) {
    return #count in obj;
  }
}
 
console.log(Counter.isCounter(new Counter()));
$ npx vite build
vite v6.4.6 building for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                0.15 kB │ gzip: 0.14 kB
dist/assets/index-C8QNsfZS.js  1.36 kB │ gzip: 0.73 kB
✓ built in 51ms
$ echo $?
0
$ grep -o "class.*console.log" dist/assets/index-C8QNsfZS.js
class m{constructor(){l(this,f);l(this,s,0)}static isCounter(e){return a(s,e)}}s=new WeakMap,f=new WeakSet,h=function(){p(this,s)._++};console.log
$ cat vite.config.js
import { defineConfig } from "vite";
 
export default defineConfig({
  build: {
    target: "modules",
  },
});
$ npx vite build
vite v6.4.6 building for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                0.15 kB │ gzip: 0.14 kB
dist/assets/index-C8QNsfZS.js  1.36 kB │ gzip: 0.73 kB
✓ built in 48ms
$ echo $?
0
$ grep -A6 "const ESBUILD_MODULES_TARGET" ../node_modules/vite/dist/node/constants.js
const ESBUILD_MODULES_TARGET = [
  "es2020",
  "edge88",
  "firefox78",
  "chrome87",
  "safari14"
];

v7 に上げると

$ npx vite build
vite v7.3.8 building client environment for production...
transforming...
✓ 3 modules transformed.
✗ Build failed in 42ms
error during build:
[vite:esbuild-transpile] Invalid target "modules" in "--target=modules"
    at /work/v7/node_modules/esbuild/lib/main.js:1001:38
    at responseCallbacks.<computed> (/work/v7/node_modules/esbuild/lib/main.js:886:9)
    at handleIncomingPacket (/work/v7/node_modules/esbuild/lib/main.js:940:31)
    at Socket.readFromStdout (/work/v7/node_modules/esbuild/lib/main.js:864:7)
    at Socket.emit (node:events:514:28)
    at addChunk (node:internal/streams/readable:569:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:520:3)
    at Readable.push (node:internal/streams/readable:400:5)
    at Pipe.onStreamRead (node:internal/stream_base_commons:194:23)
$ echo $?
1
$ rm vite.config.js
$ npx vite build
vite v7.3.8 building client environment for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                0.15 kB │ gzip: 0.14 kB
dist/assets/index-CPrUsCcf.js  0.81 kB │ gzip: 0.46 kB
✓ built in 52ms
$ echo $?
0
$ grep -o "class.*console.log" dist/assets/index-CPrUsCcf.js
class n{#e=0;#t(){this.#e++}static isCounter(r){return#e in r}}console.log

v7 で直すと

$ cat vite.config.js
import { defineConfig } from "vite";
 
export default defineConfig({
  build: {
    target: ["es2020", "edge88", "firefox78", "chrome87", "safari14"],
  },
});
$ npx vite build
vite v7.3.8 building client environment for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                0.15 kB │ gzip: 0.14 kB
dist/assets/index-C8QNsfZS.js  1.36 kB │ gzip: 0.73 kB
✓ built in 67ms
$ echo $?
0
$ grep -o "class.*console.log" dist/assets/index-C8QNsfZS.js
class m{constructor(){l(this,f);l(this,s,0)}static isCounter(e){return a(s,e)}}s=new WeakMap,f=new WeakSet,h=function(){p(this,s)._++};console.log

6 の既定では、private フィールドは WeakMap を、private メソッドは WeakSet を使う形に書き換えられていた。
7 の既定では #t(){…} や #e in r がそのまま出た。
エラーは出ないので、出力を見るか古いブラウザで開くまで気付かない。
build.target: "modules" と書いていると、6 では既定と同じ出力になり、7 では esbuild が Invalid target "modules" で止まった。

6 と同じブラウザに向けて出すなら、6 の 'modules' の中身を配列で書く。
6.4.6 の ESBUILD_MODULES_TARGET と同じ配列を書くと、6 と同じ名前のファイル(index-C8QNsfZS.js)が出て、中身も WeakMap と WeakSet を使う形に戻った。
古いブラウザの利用者がいなければ、build.target を消して 7 の既定に任せる。

Sass の legacy API が使えなくなった

6 では css.preprocessorOptions.scss.api に "legacy" と書くと、Sass を legacy API で呼んだ。
7 は legacy API の対応を外し、modern API だけで呼ぶ。
公式の移行ガイドは、api の行は消してよいと書いている。
ここでは @use "variables" の読み込み先を、legacy API の includePaths で src/styles に向けていた。

v6 では

$ cat src/app.scss
@use "variables" as *;
 
h1 {
  color: $brand;
}
$ cat vite.config.js
import { defineConfig } from "vite";
 
export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        api: "legacy",
        includePaths: ["src/styles"],
      },
    },
  },
});
$ npx vite build
vite v6.4.6 building for production...
transforming...
DEPRECATION WARNING [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0.
 
More info: https://sass-lang.com/d/legacy-js-api
 
✓ 4 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                 0.23 kB │ gzip: 0.17 kB
dist/assets/index-gakTMMY8.css  0.02 kB │ gzip: 0.04 kB
dist/assets/index-nbUahA19.js   0.71 kB │ gzip: 0.40 kB
✓ built in 206ms
$ echo $?
0
$ cat dist/assets/index-gakTMMY8.css
h1{color:#646cff}

v7 に上げると

$ npx vite build
vite v7.3.8 building client environment for production...
transforming...
✓ 3 modules transformed.
✗ Build failed in 177ms
error during build:
[vite:css] [sass] Can't find stylesheet to import.
  ╷
1 │ @use "variables" as *;
  │ ^^^^^^^^^^^^^^^^^^^^^
  ╵
  src/app.scss 1:1  root stylesheet
file: /work/v7/sass-legacy/src/app.scss
 
[sass] Can't find stylesheet to import.
  ╷
1 │ @use "variables" as *;
  │ ^^^^^^^^^^^^^^^^^^^^^
  ╵
  src/app.scss 1:1  root stylesheet
 
Error: Can't find stylesheet to import.
  ╷
1 │ @use "variables" as *;
  │ ^^^^^^^^^^^^^^^^^^^^^
  ╵
  src/app.scss 1:1  root stylesheet
    at Object.wrapException (/work/v7/node_modules/sass/sass.dart.js:2353:47)
    at /work/v7/node_modules/sass/sass.dart.js:93319:25
    at _wrapJsFunctionForAsync_closure.$protected (/work/v7/node_modules/sass/sass.dart.js:5024:15)
    at _wrapJsFunctionForAsync_closure.call$2 (/work/v7/node_modules/sass/sass.dart.js:39627:12)
    at _awaitOnObject_closure.call$1 (/work/v7/node_modules/sass/sass.dart.js:39615:32)
    at StaticClosure._rootRunUnary (/work/v7/node_modules/sass/sass.dart.js:5448:18)
    at _CustomZone.runUnary$2$2 (/work/v7/node_modules/sass/sass.dart.js:41082:39)
    at _Future__propagateToListeners_handleValueCallback.call$0 (/work/v7/node_modules/sass/sass.dart.js:40116:51)
    at Object._Future__propagateToListeners (/work/v7/node_modules/sass/sass.dart.js:5234:93)
    at _Future._completeWithValue$1 (/work/v7/node_modules/sass/sass.dart.js:39979:9)
$ echo $?
1

v7 で直すと

$ cat vite.config.js
import { defineConfig } from "vite";
 
export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        loadPaths: ["src/styles"],
      },
    },
  },
});
$ npx vite build
vite v7.3.8 building client environment for production...
transforming...
✓ 4 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                 0.23 kB │ gzip: 0.17 kB
dist/assets/index-gakTMMY8.css  0.02 kB │ gzip: 0.04 kB
dist/assets/index-nbUahA19.js   0.71 kB │ gzip: 0.40 kB
✓ built in 164ms
$ echo $?
0
$ cat dist/assets/index-gakTMMY8.css
h1{color:#646cff}

6 の build は、Sass の legacy API が非推奨だという警告(legacy-js-api)を出して通った。
7 は api: "legacy" について何も言わずに modern API で呼び、@use "variables" の読み込み先が見つからずに止まった。
modern API のオプションに includePaths は無く、書いても効かない。

modern API で同じ役目のオプションは loadPaths である。
api の行を消して includePaths を loadPaths に書き換えると、6 と同じ CSS が出た。
Sass の資料によると、legacy API の importer や関数の渡し方も modern API では形が違う。
それらを使っている設定は、このページでは打っていない。

splitVendorChunkPlugin が無くなった

splitVendorChunkPlugin は、node_modules から読んだモジュールを vendor のチャンクに分けるプラグインで、5.2.7 から非推奨だった。
7 では vite の export から無くなった。

v6 では

$ cat vite.config.js
import { defineConfig, splitVendorChunkPlugin } from "vite";
 
export default defineConfig({
  plugins: [splitVendorChunkPlugin()],
});
$ npx vite build
vite v6.4.6 building for production...
transforming...
✓ 643 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                 0.23 kB │ gzip: 0.18 kB
dist/assets/index-BXGFO8Mz.js   0.78 kB │ gzip: 0.45 kB
dist/assets/vendor-qjYRiskB.js  1.80 kB │ gzip: 0.91 kB
✓ built in 451ms
$ echo $?
0

v7 に上げると

$ npx vite build
failed to load config from /work/v7/split-vendor/vite.config.js
error during build:
file:///work/v7/node_modules/.vite-temp/vite.config.js.timestamp-1794532462361-fa28a2c42ee22.mjs:2
import { defineConfig, splitVendorChunkPlugin } from "file:///work/v7/node_modules/vite/dist/node/index.js";
                       ^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: The requested module 'file:///work/v7/node_modules/vite/dist/node/index.js' does not provide an export named 'splitVendorChunkPlugin'
    at #asyncInstantiate (node:internal/modules/esm/module_job:455:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:553:5)
    at async node:internal/modules/esm/loader:647:26
    at async loadConfigFromBundledFile (file:///work/v7/node_modules/vite/dist/node/chunks/config.js:36166:12)
    at async bundleAndLoadConfigFile (file:///work/v7/node_modules/vite/dist/node/chunks/config.js:36054:17)
    at async loadConfigFromFile (file:///work/v7/node_modules/vite/dist/node/chunks/config.js:36021:42)
    at async resolveConfig (file:///work/v7/node_modules/vite/dist/node/chunks/config.js:35670:22)
    at async createBuilder (file:///work/v7/node_modules/vite/dist/node/chunks/config.js:34125:19)
    at async CAC.<anonymous> (file:///work/v7/node_modules/vite/dist/node/cli.js:629:10)
$ echo $?
1

v7 で直すと

$ cat vite.config.js
import { defineConfig } from "vite";
 
export default defineConfig({
  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("node_modules")) {
            return "vendor";
          }
        },
      },
    },
  },
});
$ npx vite build
vite v7.3.8 building client environment for production...
transforming...
✓ 643 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                 0.23 kB │ gzip: 0.18 kB
dist/assets/index-BXGFO8Mz.js   0.78 kB │ gzip: 0.45 kB
dist/assets/vendor-qjYRiskB.js  1.80 kB │ gzip: 0.91 kB
✓ built in 459ms
$ echo $?
0

6 では警告も出ずに vendor のチャンクが分かれた。
7 は設定ファイルを読み込むところで、splitVendorChunkPlugin という export が無いという SyntaxError になり、build に入る前に止まった。

移行ガイドのとおり build.rollupOptions.output.manualChunks で分ける。
関数形式で node_modules を含むパスを vendor に入れると、6 と同じ名前の 2 つのファイルが出た。
このページの依存は lodash-es だけで、動的 import を使うコードでの分かれ方は打っていない。
8 に上げるときは分割の設定がまた変わる(Vite 7 → 8 の manualChunks の節)。

transformIndexHtml の enforce と transform が使えなくなった

transformIndexHtml フックはオブジェクトでも書け、順番を order に、処理を handler に書く。
同じ役目の古い名前 enforce と transform は 4.0.0 から非推奨で、7 で無くなった。

v6 では

$ cat vite.config.js
import { defineConfig } from "vite";
 
function htmlTitle() {
  return {
    name: "html-title",
    transformIndexHtml: {
      enforce: "pre",
      transform(html) {
        return html.replace("%APP_TITLE%", "My App");
      },
    },
  };
}
 
export default defineConfig({
  plugins: [htmlTitle()],
});
$ npx vite build
plugin 'html-title' uses deprecated 'enforce' option. Use 'order' option instead.
plugin 'html-title' uses deprecated 'transform' option. Use 'handler' option instead.
vite v6.4.6 building for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                0.18 kB │ gzip: 0.14 kB
dist/assets/index-Oi0r7DEU.js  0.74 kB │ gzip: 0.42 kB
✓ built in 50ms
$ echo $?
0
$ grep "<title>" dist/index.html
    <title>My App</title>

v7 に上げると

$ npx vite build
vite v7.3.8 building client environment for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
✗ Build failed in 44ms
error during build:
[vite:build-html] Cannot read properties of undefined (reading 'call')
    at applyHtmlTransforms (file:///work/v7/node_modules/vite/dist/node/chunks/config.js:24580:26)
    at Object.generateBundle (file:///work/v7/node_modules/vite/dist/node/chunks/config.js:24388:20)
    at Object.handler (file:///work/v7/node_modules/vite/dist/node/chunks/config.js:33993:13)
    at file:///work/v7/node_modules/rollup/dist/es/shared/node-entry.js:24136:40
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5)
$ echo $?
1

v7 で直すと

$ cat vite.config.js
import { defineConfig } from "vite";
 
function htmlTitle() {
  return {
    name: "html-title",
    transformIndexHtml: {
      order: "pre",
      handler(html) {
        return html.replace("%APP_TITLE%", "My App");
      },
    },
  };
}
 
export default defineConfig({
  plugins: [htmlTitle()],
});
$ npx vite build
vite v7.3.8 building client environment for production...
transforming...
✓ 3 modules transformed.
rendering chunks...
computing gzip size...
dist/index.html                0.18 kB │ gzip: 0.14 kB
dist/assets/index-Oi0r7DEU.js  0.74 kB │ gzip: 0.42 kB
✓ built in 53ms
$ echo $?
0
$ grep "<title>" dist/index.html
    <title>My App</title>

6 は非推奨の警告を 2 行出して、%APP_TITLE% を置き換えた。
7 は警告を出さず、HTML を処理するところで Cannot read properties of undefined (reading 'call') になった。
エラー文に出る vite:build-html は Vite 自身のプラグインで、どのプラグインの書き方が原因かは書かれていない。
6 のうちに build すると、警告にプラグインの名前(ここでは html-title)が出る。

enforce を order に、transform を handler に書き換えると、7 でも <title> が置き換わった。
npm から入れたプラグインが原因なら、プラグインを上げる(このページでは打っていない)。

configureServer で足したルートにも Host の検査と CORS が効くようになった

dev サーバーには、許可していない Host ヘッダーのリクエストを断る検査(server.allowedHosts)と、CORS のヘッダーを付ける処理(server.cors)がある。
7 はこの 2 つを、プラグインが configureServer で足したミドルウェアより前に置くようになった。
ここでは JSON を返すモックの API /api/hello をプラグインで足し、Host と Origin のヘッダーを付けて curl で打った。

v6 では

$ cat vite.config.js
import { defineConfig } from "vite";
 
function mockApi() {
  return {
    name: "mock-api",
    configureServer(server) {
      server.middlewares.use("/api/hello", (req, res) => {
        res.setHeader("Content-Type", "application/json");
        res.end(JSON.stringify({ message: "hello" }));
      });
    },
  };
}
 
export default defineConfig({
  plugins: [mockApi()],
});
$ npx vite > vite.log 2>&1 &
$ curl -s -H "Host: myapp.example.com" http://localhost:5173/
Blocked request. This host ("myapp.example.com") is not allowed.
To allow this host, add "myapp.example.com" to `server.allowedHosts` in vite.config.js.
$ curl -s -H "Host: myapp.example.com" http://localhost:5173/api/hello
{"message":"hello"}
$ curl -s -o /dev/null -D - -H "Origin: http://localhost:3000" http://localhost:5173/api/hello
HTTP/1.1 200 OK
Content-Type: application/json
Date: Fri, 13 Nov 2026 01:14:24 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 19

v7 に上げると

$ npx vite > vite.log 2>&1 &
$ curl -s -H "Host: myapp.example.com" http://localhost:5173/
Blocked request. This host ("myapp.example.com") is not allowed.
To allow this host, add "myapp.example.com" to `server.allowedHosts` in vite.config.js.
$ curl -s -H "Host: myapp.example.com" http://localhost:5173/api/hello
Blocked request. This host ("myapp.example.com") is not allowed.
To allow this host, add "myapp.example.com" to `server.allowedHosts` in vite.config.js.
$ curl -s -o /dev/null -D - -H "Origin: http://localhost:3000" http://localhost:5173/api/hello
HTTP/1.1 200 OK
Access-Control-Allow-Origin: http://localhost:3000
Vary: Origin
Content-Type: application/json
Date: Fri, 13 Nov 2026 01:14:25 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 19

v7 で直すと

$ cat vite.config.js
import { defineConfig } from "vite";
 
function mockApi() {
  return {
    name: "mock-api",
    configureServer(server) {
      server.middlewares.use("/api/hello", (req, res) => {
        res.removeHeader("Access-Control-Allow-Origin");
        res.setHeader("Content-Type", "application/json");
        res.end(JSON.stringify({ message: "hello" }));
      });
    },
  };
}
 
export default defineConfig({
  plugins: [mockApi()],
  server: {
    allowedHosts: ["myapp.example.com"],
  },
});
$ npx vite > vite.log 2>&1 &
$ curl -s -H "Host: myapp.example.com" http://localhost:5173/
<!doctype html>
<html>
<script type="module" src="/@vite/client"></script>
 
  <body>
    <div id="app"></div>
    <script type="module" src="/src/main.js"></script>
  </body>
</html>
$ curl -s -H "Host: myapp.example.com" http://localhost:5173/api/hello
{"message":"hello"}
$ curl -s -o /dev/null -D - -H "Origin: http://localhost:3000" http://localhost:5173/api/hello
HTTP/1.1 200 OK
Vary: Origin
Content-Type: application/json
Date: Fri, 13 Nov 2026 01:14:25 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 19

6 でも / は Host の検査で断られたが、プラグインの /api/hello は応答した。
7 では /api/hello も同じ文言で断られた。
Access-Control-Allow-Origin も、6 では付かなかったのに、7 では Origin の値で付いた。
server.allowedHosts に無いホスト名でモックの API だけを呼んでいると、7 に上げたところで断られる。
公式のドキュメントによると、localhost と .localhost のサブドメイン、IP アドレスは既定で通る(このページでは打っていない)。

移行ガイドの注記は、CORS のヘッダーにだけ触れている。
変更の PR(vitejs/vite#20222(新しいタブで開く))の説明によると、前に移したのは CORS と Host の検査のほか、計時(timeMiddleware)と、不正なリクエストを断るミドルウェア(rejectInvalidRequestMiddleware)を合わせた 4 つである。

Host は、エラー文のとおり server.allowedHosts に足す。
足すと、/ と /api/hello の両方が応答した。

CORS のヘッダーを付けたくないルートは、PR の説明にあるとおり、レスポンスから Access-Control-Allow-Origin を消す。
消したあとは Vary: Origin だけが残った。

vitejs/vite#20224(新しいタブで開く) によると、プレビューサーバーの configurePreviewServer でも CORS と Host の検査が前に移った。
プレビューサーバーは、このページでは打っていない。

検証環境Node.js 24.22.3 / npm 11.19.1 / Python 3.11.2 / 2026-11-13 検証(ほか 15 件)
検証日
実行環境
node:24 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 24.22.3
  • npm 11.19.1
  • Python 3.11.2
  • Git 2.39.5
  • Vite(旧) 6.4.6
  • Vite(新) 7.3.8
  • esbuild(旧の同梱) 0.25.12
  • esbuild(新の同梱) 0.28.2
  • Rollup(旧の同梱) 4.68.2
  • Rollup(新の同梱) 4.68.2
  • Sass 1.108.2
  • lodash-es 4.18.1
  • Node.js(18 の段) 18.20.8
  • npm(Node.js 18 に同梱) 10.8.2
  • Node.js(20.18 の段) 20.18.3
  • Node.js(20.19 の段) 20.19.0
  • @types/node(旧) 18.19.130
  • @types/node(新) 24.19.2

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。