できない.dev

Babel を 7 から 8 に上げると動かなくなること

Babel を 7 から 8 に上げると、改名前の @babel/plugin-proposal-* が入ったままの npm install が ERESOLVE で止まり、preset-env の useBuiltIns と loose、コールバック無しの babel.transform がエラーになる。
TypeScript では初期値の無いクラスフィールドが undefined で上書きされる。
7 と 8 で打ち比べた 11 の変更点と直し方をまとめる。

公開: 更新:

@babel/core・@babel/cli・@babel/preset-env・@babel/preset-react・@babel/preset-typescript を 7 で入れたプロジェクトと、同じ顔ぶれを 8 で入れたプロジェクトを並べ、同じ babel.config.json とソースで同じコマンドを打ち比べた。
各節の出力は「v7 では」「v8 に上げると」「v8 で直すと」の 3 段で並ぶ。
7 の出力の先頭に "use strict"; があったり import が require になっていたりするのは、どの節も「@babel/cli の出力が CommonJS でなくなった」の節の変化による。

8 のパッケージは ES モジュールだけで配られ、npm の engines は Node.js ^22.18.0 || >=24.11.0 を求める。
この範囲に入らない Node.js(20 以前、22.17 以前の 22 など)は対象外になる。

変換した結果を古い Node.js やブラウザで動かすことは、今までどおり targets で指定できる。
今回の Node.js 24 では、CommonJS のファイルから require("@babel/core") で読み込んでも動いた。

動かなくなる原因は 3 つに分かれる。
1 つ目は消えたパッケージとオプションで、7.22 で改名された @babel/plugin-proposal-*・preset-env の useBuiltIns と loose・preset-typescript の isTSX と allExtensions が当たる。
公式の移行ガイドが 7 のうちに済ませる準備として勧める bugfixes・allowDeclareFields・useSpread も、8 では書くと止まる。
2 つ目はコールバックを渡さない babel.transform で、8 では呼んだ時点で例外になる。

3 つ目は既定の変化で、設定を変えなくても出力が変わる。
このページでは次の 5 つを打った。

  • @babel/cli の出力が CommonJS に変換されなくなる
  • import type で書いていない型の import が出力に残る
  • 初期値の無いクラスフィールドが出力に残る
  • targets を書かないと古い構文に変換されなくなる
  • JSX が開発用の react/jsx-dev-runtime の呼び出しになる

このうち型の import とクラスフィールドは TypeScript の変換で起きる。
前者は読み込みのエラーになり、後者はエラーの出ない値の違いになる。

上げる前に、設定ファイルと package.json を次の語で検索しておく。
plugin-proposal・useBuiltIns・loose・bugfixes・isTSX・allExtensions・allowDeclareFields・useSpread である。
plugin-proposal で見つかったもののうち、改名されていない decorators などは名前のまま 8 に上げる。
コードも babel.transform( で検索すると、8 で止まる場所が先に分かる。

このページで打っていない変更もある。
@babel/plugin-transform-runtime の corejs が無くなったこと、デコレーターの version が必須になったこと、@babel/plugin-syntax-import-assertions などのパッケージの廃止、ESLint 8 以前のサポート終了が当たる。
targets.esmodules の扱いや transform-runtime のオプションの変更なども打っていない。
ほかの変更も含めた一覧は公式の移行ガイドにある。

preset-react の開発用の出力は、8 では jsxDEV に __source と __self を渡さなくなった。
React 19.2 より前や Preact でこれが要るなら、preset-react に developmentSourceSelf: true を書くと移行ガイドにある(このページでは打っていない)。

改名前の @babel/plugin-proposal-* が残っていると npm install が止まる

標準になった構文のプラグインは、7.22 で @babel/plugin-proposal-* から @babel/plugin-transform-* に名前が変わった。
標準になっていない構文のもの(@babel/plugin-proposal-decorators など)は改名されておらず、plugin-proposal-* の名前のまま 8 が出ている。
旧名のパッケージは 7.x のまま更新が止まっており、8 は出ていない。
peerDependencies が @babel/core の 7 を求めるので、旧名が残ったまま @babel/core を 8 に上げると、npm install が止まる。

v7 では

$ cat babel.config.json
{
  "plugins": ["@babel/plugin-proposal-class-properties"]
}
$ npx babel src/counter.js
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, e) { if ("object" != typeof t || !t) return t; var r; if ("undefined" != typeof Symbol && void 0 !== (r = t[Symbol.toPrimitive])) { var i = r.call(t, e || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === e ? String : Number)(t); }
class Counter {
  constructor() {
    _defineProperty(this, "count", 0);
  }
}
_defineProperty(Counter, "label", "clicks");
$ echo $?
0

v8 に上げると

$ npm install -D @babel/core@8 @babel/cli@8
npm error code ERESOLVE
npm error ERESOLVE could not resolve
npm error
npm error While resolving: proposal@1.0.0
npm error Found: @babel/cli@7.29.7
npm error node_modules/@babel/cli
npm error   dev @babel/cli@"8" from the root project
npm error
npm error Could not resolve dependency:
npm error dev @babel/cli@"8" from the root project
npm error
npm error Conflicting peer dependency: @babel/core@8.1.0
npm error node_modules/@babel/core
npm error   peer @babel/core@"^8.0.0" from @babel/cli@8.1.0
npm error   node_modules/@babel/cli
npm error     dev @babel/cli@"8" 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-07T22_44_05_202Z-eresolve-report.txt
npm error A complete log of this run can be found in: /root/.npm/_logs/2026-11-07T22_44_05_202Z-debug-0.log
$ echo $?
1
$ npm view @babel/plugin-proposal-class-properties peerDependencies
{ '@babel/core': '^7.0.0-0' }
$ npm view @babel/plugin-proposal-class-properties dist-tags
{ latest: '7.18.6', esm: '7.21.4-esm.4' }

v8 で直すと

$ npm uninstall @babel/plugin-proposal-class-properties
removed 8 packages in 157ms
$ echo $?
0
$ npm install -D @babel/core@8 @babel/cli@8 @babel/plugin-transform-class-properties@8
added 18 packages, removed 31 packages, and changed 26 packages in 664ms
$ echo $?
0
$ cat babel.config.json
{
  "plugins": ["@babel/plugin-transform-class-properties"]
}
$ npx babel src/counter.js
function _defineProperty(r, t, o) { return (t = _toPropertyKey(t)) in r ? Object.defineProperty(r, t, { value: o, enumerable: !0, configurable: !0, writable: !0 }) : r[t] = o, r; }
function _toPropertyKey(r) { var o = _toPrimitive(r, "string"); return "symbol" == typeof o ? o : o + ""; }
function _toPrimitive(t, e) { if ("object" != typeof t || !t) return t; var r; if ("undefined" != typeof Symbol && void 0 !== (r = t[Symbol.toPrimitive])) { var i = r.call(t, e || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === e ? String : Number)(t); }
class Counter {
  constructor() {
    _defineProperty(this, "count", 0);
  }
}
_defineProperty(Counter, "label", "clicks");
$ echo $?
0

npm のエラーは @babel/cli と @babel/core の組み合わせを指していて、旧名のプラグインには触れていない。
原因は npm view で見た peerDependencies のほうにある。
旧名のプラグインは ^7.0.0-0 を求め、latest も 7.18.6 のままである。

旧名のパッケージを外し、新しい名前の 8 を入れ直すと通る。
設定ファイルのプラグイン名も plugin-transform-* に書き換える。
クラスの変換の結果は 7 と同じで、違うのはヘルパーの引数の名前だけだった。
対応表は公式の移行ガイドにあり、@babel/plugin-proposal-class-properties なら @babel/plugin-transform-class-properties になる。

preset-env が既に含むプラグインであれば、設定から消してしまってもよい。
ただし 8 の preset-env は targets の既定が変わったので、古いブラウザ向けの変換が要るなら targets を書く(「targets を書かないと古い構文に変換されなくなった」の節)。
preset-env の include と exclude に旧名を書いているなら、そこも新しい名前に書き換えるよう移行ガイドにある(このページでは打っていない)。

preset-env の useBuiltIns が無くなった

7 の preset-env は、useBuiltIns と corejs を書くと、コードが使っている機能の polyfill を core-js から読み込む行を差し込んだ。
8 ではこの働きが別のプラグイン babel-plugin-polyfill-corejs3 に移り、preset-env に useBuiltIns: "usage" を書くと止まる。

v7 では

$ cat babel.config.json
{
  "presets": [
    ["@babel/preset-env", { "targets": "chrome 60", "useBuiltIns": "usage", "corejs": 3 }]
  ]
}
$ npx babel src/flat.js
"use strict";
 
require("core-js/modules/es.array.flat.js");
require("core-js/modules/es.array.unscopables.flat.js");
console.log([[1], [2, 3]].flat());
$ echo $?
0

v8 に上げると

$ npx babel src/flat.js > babel.log 2>&1
$ echo $?
1
$ head -n 1 babel.log
Error: [BABEL] /work/v8/use-built-ins/src/flat.js: The 'useBuiltIns' option has been removed. Please use babel-plugin-polyfill-corejs3 instead. (While processing: "/work/v8/node_modules/@babel/preset-env/lib/index.js")

v8 で直すと

$ cat babel.config.json
{
  "presets": [["@babel/preset-env", { "targets": "chrome 60" }]],
  "plugins": [["polyfill-corejs3", { "method": "usage-global", "version": "3.51.0" }]]
}
$ npx babel src/flat.js
console.log([[1], [2, 3]].flat());
$ echo $?
0
$ cat babel.config.json
{
  "targets": "chrome 60",
  "presets": ["@babel/preset-env"],
  "plugins": [["polyfill-corejs3", { "method": "usage-global", "version": "3.51.0" }]]
}
$ npx babel src/flat.js
import "core-js/modules/es.array.flat.js";
import "core-js/modules/es.array.unscopables.flat.js";
console.log([[1], [2, 3]].flat());
$ echo $?
0

useBuiltIns を外し、babel-plugin-polyfill-corejs3 を plugins に足すと通る。
"usage" に当たるのは method: "usage-global"、"entry" に当たるのは method: "entry-global" である。
version には、入っている core-js の版をそのまま書いた。
公式の移行ガイドによると、7 の corejs: 3 は 3.0 とみなされ、3.0 の後に足された機能の polyfill は差し込まれない。

targets は設定の最上位に置く。
1 回目のように preset-env の中に残すと、エラーは出ないが polyfill の行が差し込まれなかった。
2 回目に最上位へ移すと、flat の polyfill を読み込む行が入った。
プラグインが見るのは最上位の targets で、8 ではその既定が browserslist の defaults になる。

公式の移行ガイドは、冒頭の「Getting ready」で useBuiltIns を外して babel-plugin-polyfill-corejs3 に移すよう勧めている。
preset-env の変更点の節には useBuiltIns の削除が載っておらず、corejs に minor の版まで書くこととだけある。
8.0.1 のリリースノートに「Actually remove preset-env's useBuiltIns」とあり、8.0.1 から削除が入った。
今回入った preset-env 8.0.7 では、"usage" を書くとこのエラーになった。
"entry" と false は打っていない。

preset-env の loose が無くなり assumptions に替わった

7 の preset-env の loose: true は、中のプラグインにまとめて「仕様どおりでなくてよいので短く出す」を指示するオプションだった。
8 では preset-env の loose と spec が無くなり、書くと止まる。
代わりに、何を省いてよいかを最上位の assumptions で 1 つずつ書く。

v7 では

$ cat babel.config.json
{
  "presets": [["@babel/preset-env", { "targets": "chrome 70", "loose": true }]]
}
$ npx babel src/counter.js
"use strict";
 
class Counter {
  constructor() {
    this.count = 0;
  }
}
console.log(new Counter().count);
$ echo $?
0

v8 に上げると

$ npx babel src/counter.js > babel.log 2>&1
$ echo $?
1
$ head -n 1 babel.log
Error: [BABEL] /work/v8/preset-env-loose/src/counter.js: @babel/preset-env: The 'loose' and 'spec' options have been removed, and you should configure granular compiler assumptions instead. See https://babeljs.io/assumptions for more information. (While processing: "/work/v8/node_modules/@babel/preset-env/lib/index.js")

v8 で直すと

$ cat babel.config.json
{
  "assumptions": { "setPublicClassFields": true },
  "presets": [["@babel/preset-env", { "targets": "chrome 70" }]]
}
$ npx babel src/counter.js
class Counter {
  constructor() {
    this.count = 0;
  }
}
console.log(new Counter().count);
$ echo $?
0

例のクラスのフィールドは、7 の loose では this.count = 0 になった。
8 で同じ出力を得るには assumptions の setPublicClassFields を true にする。

loose が効いていたものはクラスのほかにスプレッドや分割代入などにも及ぶので、どの assumptions に当たるかは公式の assumptions のページで 1 つずつ確かめる。
個々のプラグインに直接書いた loose は、移行ガイドによると 8 でも警告つきで動く。

preset-typescript の isTSX と allExtensions が無くなった

7 の preset-typescript は、allExtensions: true で拡張子に関係なく全部のファイルを TypeScript として読み、isTSX: true で JSX も読んだ。
8 ではこの 2 つが無くなり、書くと止まる。
エラーの文が代わりの書き方を示している。

v7 では

$ cat babel.config.json
{
  "presets": [["@babel/preset-typescript", { "isTSX": true, "allExtensions": true }]]
}
$ npx babel src/badge.ts
export const badge = name => <b>{name}</b>;
$ echo $?
0

v8 に上げると

$ npx babel src/badge.ts > babel.log 2>&1
$ echo $?
1
$ head -n 3 babel.log
Error: [BABEL] /work/v8/typescript-istsx/src/badge.ts: @babel/preset-typescript: The .allExtensions and .isTSX options have been removed.
If you want to disable JSX detection based on file extensions, you can set the .ignoreExtensions option to true.
If you want to force JSX parsing, you can enable the @babel/plugin-syntax-jsx plugin. (While processing: "/work/v8/node_modules/@babel/preset-typescript/lib/index.js")

v8 で直すと

$ cat babel.config.json
{
  "presets": [["@babel/preset-typescript", { "ignoreExtensions": true }]],
  "plugins": ["@babel/plugin-syntax-jsx"]
}
$ npx babel src/badge.ts
export const badge = name => <b>{name}</b>;
$ echo $?
0

拡張子で JSX の有無を決めない ignoreExtensions: true を書き、JSX を読ませるために @babel/plugin-syntax-jsx を足すと、7 と同じ出力になった。
公式の移行ガイドによると、preset-react などの JSX のプリセットも使っていて、JSX を .tsx のファイルだけに書いているなら、2 つのオプションを消すだけでよい(このページでは打っていない)。
どちらも false で書いていた場合も、消すだけでよい。

8 への備えとして 7 で足す bugfixes・allowDeclareFields・useSpread が 8 では止まる

公式の移行ガイドは、8 に上げる前に 7 のまま済ませておく準備を挙げている。
そのうち 3 つは、8 で無くなるオプションを 7 で足しておくものだ。
preset-env を使っているなら bugfixes を true にする。
TypeScript を変換しているなら preset-typescript の allowDeclareFields を true にし、preset-react を classic のまま使うなら useSpread を true にする。
どれも 8 の既定の動きを 7 で先に使うためのオプションで、8 では書いてあるだけで止まる。
準備のとおりに設定してから上げると、ここで止まることになる。

v7 では

$ cat ts.babel.json
{
  "presets": [["@babel/preset-typescript", { "allowDeclareFields": true }]]
}
$ npx babel --config-file ./ts.babel.json src/user.ts
class User {}
$ echo $?
0
$ cat react.babel.json
{
  "presets": [["@babel/preset-react", { "runtime": "classic", "useSpread": true }]]
}
$ npx babel --config-file ./react.babel.json src/link.jsx
export const Link = props => /*#__PURE__*/React.createElement("a", {
  ...props
});
$ echo $?
0
$ cat env.babel.json
{
  "presets": [["@babel/preset-env", { "targets": "defaults", "bugfixes": true }]]
}
$ npx babel --config-file ./env.babel.json src/add.js
"use strict";
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.add = void 0;
const add = (a, b = 1) => a + b;
exports.add = add;
$ echo $?
0

v8 に上げると

$ npx babel --config-file ./ts.babel.json src/user.ts > babel.log 2>&1
$ echo $?
1
$ head -n 1 babel.log
Error: [BABEL] /work/v8/prep-options/src/user.ts: @babel/preset-typescript: The .allowDeclareFields option has been removed and it's now always enabled. Please remove it from your config. (While processing: "/work/v8/node_modules/@babel/preset-typescript/lib/index.js")
$ npx babel --config-file ./react.babel.json src/link.jsx > babel.log 2>&1
$ echo $?
1
$ head -n 1 babel.log
Error: [BABEL] /work/v8/prep-options/src/link.jsx: @babel/preset-react: Since Babel 8, an inline object with spread elements is always used, and the "useSpread" option is no longer available. Please remove it from your config. (While processing: "/work/v8/node_modules/@babel/preset-react/lib/index.js")
$ npx babel --config-file ./env.babel.json src/add.js > babel.log 2>&1
$ echo $?
1
$ head -n 1 babel.log
Error: [BABEL] /work/v8/prep-options/src/add.js: @babel/preset-env: The 'bugfixes' option has been removed, and now bugfix plugins are always enabled. Please remove it from your config. (While processing: "/work/v8/node_modules/@babel/preset-env/lib/index.js")

v8 で直すと

$ cat ts.babel.json
{
  "presets": ["@babel/preset-typescript"]
}
$ npx babel --config-file ./ts.babel.json src/user.ts
class User {}
$ echo $?
0
$ cat react.babel.json
{
  "presets": [["@babel/preset-react", { "runtime": "classic" }]]
}
$ npx babel --config-file ./react.babel.json src/link.jsx
export const Link = props => /*#__PURE__*/React.createElement("a", props);
$ echo $?
0
$ cat env.babel.json
{
  "presets": [["@babel/preset-env", { "targets": "defaults" }]]
}
$ npx babel --config-file ./env.babel.json src/add.js
export const add = (a, b = 1) => a + b;
$ echo $?
0

3 つとも消すと通る。
エラーの文のとおり、allowDeclareFields が有効にしていた declare のフィールドと、bugfixes が足していた変換は、8 では常に有効になる。
useSpread については、8 ではスプレッドの書き方が常に使われるとエラーの文にある。
今回の例は {...props} だけだったので、8 の出力では props がそのまま渡り、7 の出力とは形が違った。

コールバックを渡さない babel.transform が使えなくなった

7 の babel.transform は、コールバックを渡さなければ結果をそのまま返した。
8 ではコールバックが必須になり、渡さないと呼んだ行で例外になる。
transformFile・transformFromAst・parse・loadOptions・loadPartialConfig・createConfigItem も同じである。

v7 では

$ cat transform.cjs
const babel = require("@babel/core");
 
const result = babel.transform("const add = (a, b) => a + b;", {
  configFile: false,
  presets: [["@babel/preset-env", { targets: "ie 11" }]],
});
console.log(result.code);
$ node transform.cjs
"use strict";
 
var add = function add(a, b) {
  return a + b;
};
$ echo $?
0

v8 に上げると

$ node transform.cjs
file:///work/v8/node_modules/@babel/core/lib/index-shared.js:2873
    throw new Error("Starting from Babel 8.0.0, the 'transform' function expects a callback. If you need to call it synchronously, please use 'transformSync'.");
    ^
 
Error: Starting from Babel 8.0.0, the 'transform' function expects a callback. If you need to call it synchronously, please use 'transformSync'.
    at Module.transform (file:///work/v8/node_modules/@babel/core/lib/index-shared.js:2873:11)
    at Object.<anonymous> (/work/v8/transform-callback/transform.cjs:3:22)
    at Module._compile (node:internal/modules/cjs/loader:1929:14)
    at Object..js (node:internal/modules/cjs/loader:2060:10)
    at Module.load (node:internal/modules/cjs/loader:1651:32)
    at Module._load (node:internal/modules/cjs/loader:1443:12)
    at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
    at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
    at node:internal/main/run_main_module:33:47
 
Node.js v24.22.2
$ echo $?
1

v8 で直すと

$ cat transform.cjs
const babel = require("@babel/core");
 
const result = babel.transformSync("const add = (a, b) => a + b;", {
  configFile: false,
  presets: [["@babel/preset-env", { targets: "ie 11" }]],
});
console.log(result.code);
$ node transform.cjs
var add = function add(a, b) {
  return a + b;
};
$ echo $?
0

transformSync に書き換えると通る。
ほかの関数も、名前の末尾に Sync の付いたもの(parseSync・loadOptionsSync など)に替える。
await できる場所なら transformAsync でもよい。

@babel/cli の出力が CommonJS でなくなった

preset-env の modules の既定は "auto" で、7 では @babel/cli から呼ぶと import と export を CommonJS の require に書き換えた。
8 の "auto" は呼び出し元が ES モジュールを扱えるものとみなし、import を残す。
package.json の "type" が "commonjs" のプロジェクトでは、出力した .js が Node.js で動かなくなる。
npm 11 の npm init -y は "type": "commonjs" を書くので、このページのプロジェクトもこの形である。
"type" の欄が無いプロジェクトは打っていない。

v7 では

$ cat babel.config.json
{
  "presets": [["@babel/preset-env", { "targets": "node 20" }]]
}
$ npx babel src -d dist
Successfully compiled 1 file with Babel (98ms).
$ echo $?
0
$ cat dist/index.js
"use strict";
 
var _nodePath = _interopRequireDefault(require("node:path"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
console.log(_nodePath.default.join("dist", "index.js"));
$ node dist/index.js
dist/index.js
$ echo $?
0

v8 に上げると

$ npx babel src -d dist
Successfully compiled 1 file with Babel (60ms).
$ echo $?
0
$ cat dist/index.js
import path from "node:path";
console.log(path.join("dist", "index.js"));
$ npm pkg get type
"commonjs"
$ node dist/index.js
(node:986) Warning: Failed to load the ES module: /work/v8/modules-auto/dist/index.js. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/work/v8/modules-auto/dist/index.js:1
import path from "node:path";
^^^^^^
 
SyntaxError: Cannot use import statement outside a module
    at wrapSafe (node:internal/modules/cjs/loader:1861:18)
    at Module._compile (node:internal/modules/cjs/loader:1903:20)
    at Object..js (node:internal/modules/cjs/loader:2060:10)
    at Module.load (node:internal/modules/cjs/loader:1651:32)
    at Module._load (node:internal/modules/cjs/loader:1443:12)
    at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
    at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
    at node:internal/main/run_main_module:33:47
 
Node.js v24.22.2
$ echo $?
1

v8 で直すと

$ cat babel.config.json
{
  "presets": [["@babel/preset-env", { "targets": "node 20", "modules": "commonjs" }]]
}
$ npx babel src -d dist
Successfully compiled 1 file with Babel (68ms).
$ echo $?
0
$ node dist/index.js
dist/index.js
$ echo $?
0

v8 の出力には import が残り、"type" が "commonjs" なので Node.js は CommonJS として読もうとして止まった。
preset-env に modules: "commonjs" を書くと、7 と同じく require に書き換えられ、動いた。
公式の移行ガイドは、CommonJS に変換したいときの書き方としてこの modules: "commonjs" と、CommonJS に変換するプラグインを挙げている。
ガイドの表記は @babel/plugin-transform-commonjs だが、npm にこの名前は無く、パッケージ名は @babel/plugin-transform-modules-commonjs である。

webpack や Rollup から Babel を呼んでいる場合は、ツールが ES モジュールを扱えると Babel に伝えるので、この変化の影響を受けないとも書いている。

import type で書いていない型の import が出力に残る

TypeScript の import { User } from "./types.js" は、User が型だけなら JavaScript に要らない。
7 の preset-typescript は、値として使われていない import を出力から消した。
8 では onlyRemoveTypeImports の既定が true になり、消すのは import type で書いた import だけになった。
型だけを export するファイルは出力が空になるので、ES モジュールとして動かすと、残った import が読み込みの時点で止まる。

v7 では

$ cat src/index.ts
import { User } from "./types.js";
 
const user: User = { name: "alice" };
console.log(user.name);
$ cat dist/package.json
{ "type": "module" }
$ npx babel src -d dist --extensions .ts
Successfully compiled 2 files with Babel (65ms).
$ echo $?
0
$ cat dist/index.js
const user = {
  name: "alice"
};
console.log(user.name);
export {};
$ node dist/index.js
alice
$ echo $?
0

v8 に上げると

$ npx babel src -d dist --extensions .ts
Successfully compiled 2 files with Babel (41ms).
$ echo $?
0
$ cat dist/index.js
import { User } from "./types.js";
const user = {
  name: "alice"
};
console.log(user.name);
$ node dist/index.js > babel.log 2>&1
$ echo $?
1
$ head -n 4 babel.log
file:///work/v8/type-only-imports/dist/index.js:1
import { User } from "./types.js";
         ^^^^
SyntaxError: The requested module './types.js' does not provide an export named 'User'

v8 で直すと

$ cat src/index.ts
import type { User } from "./types.js";
 
const user: User = { name: "alice" };
console.log(user.name);
$ npx babel src -d dist --extensions .ts
Successfully compiled 2 files with Babel (41ms).
$ echo $?
0
$ node dist/index.js
alice
$ echo $?
0

型の import を import type で書くと、8 でも出力から消え、動いた。
公式の移行ガイドは、TypeScript 3.8 より前を使っているか、tsconfig の verbatimModuleSyntax を true にしていないなら、onlyRemoveTypeImports: false を書くよう勧めている。

初期値の無いクラスフィールドが undefined で上書きされる

7 の preset-typescript は、name: string; のように型だけを書いた初期値の無いクラスフィールドを、出力から消した。
8 はこれを JavaScript のクラスフィールドとして残す。
クラスフィールドは親クラスのコンストラクタが終わったあとに undefined で定義されるので、親クラスのコンストラクタで入れた値が上書きされる。
エラーは出ない。
公式の移行ガイドは、この変更の危険度を「高」としている。

v7 では

$ cat src/user.ts
class Model {
  constructor(data: Record<string, unknown>) {
    Object.assign(this, data);
  }
}
 
class User extends Model {
  name: string;
}
 
console.log(new User({ name: "alice" }).name);
$ npx babel src/user.ts -o user.js
$ echo $?
0
$ node user.js
alice
$ echo $?
0

v8 に上げると

$ npx babel src/user.ts -o user.js
$ echo $?
0
$ cat user.js
class Model {
  constructor(data) {
    Object.assign(this, data);
  }
}
class User extends Model {
  name;
}
console.log(new User({
  name: "alice"
}).name);
$ node user.js
undefined
$ echo $?
0

v8 で直すと

$ cat src/user.ts
class Model {
  constructor(data: Record<string, unknown>) {
    Object.assign(this, data);
  }
}
 
class User extends Model {
  declare name: string;
}
 
console.log(new User({ name: "alice" }).name);
$ npx babel src/user.ts -o user.js
$ echo $?
0
$ node user.js
alice
$ echo $?
0

v8 の出力には name; が残り、値が undefined になった。
型だけを書きたいフィールドに declare を付けると、8 でも出力から消え、7 と同じ値になった。
7 で declare を使うには preset-typescript の allowDeclareFields: true が要った。
8 では既定で使える(「8 への備えとして 7 で足す bugfixes・allowDeclareFields・useSpread が 8 では止まる」の節)。
ORM のモデルや、親クラスがデータを流し込むクラスで起きやすい。

targets を書かないと古い構文に変換されなくなった

preset-env は targets で対象のブラウザや Node.js を決め、そこで動かない構文だけを変換する。
targets も browserslist の設定も無いとき、7 は全部の構文を ES5 まで変換した。
8 は browserslist の defaults(主要ブラウザの新しい版)を対象にするので、アロー関数・テンプレートリテラル・?? がそのまま残る。
エラーは出ない。

v7 では

$ cat babel.config.json
{
  "presets": [["@babel/preset-env", { "modules": false }]]
}
$ npx babel src/greet.js
export var greet = function greet(name) {
  return "Hello, ".concat(name !== null && name !== void 0 ? name : "guest");
};
$ echo $?
0

v8 に上げると

$ npx babel src/greet.js
export const greet = name => `Hello, ${name ?? "guest"}`;
$ echo $?
0

v8 で直すと

$ cat babel.config.json
{
  "presets": [["@babel/preset-env", { "modules": false, "targets": ">= 0%" }]]
}
$ npx babel src/greet.js
export var greet = function greet(name) {
  return "Hello, ".concat(name !== null && name !== void 0 ? name : "guest");
};
$ echo $?
0

公式の移行ガイドによると、7 の既定は ">= 0%"(すべてのブラウザ)で、8 の既定は "defaults" である。
targets に ">= 0%" を書くと、7 と同じ出力に戻った。
ガイドは、.browserslistrc を使っていないなら最上位に targets を書いておくよう勧めている。
動かす環境が決まっているなら、その環境を書くと要らない変換が減る。
公式の移行ガイドは、この変更の危険度を「高」としている。

preset-react の既定が automatic になり開発用の出力が出る

7 の preset-react は、JSX を React.createElement の呼び出しに変換した(runtime: "classic")。
8 は runtime の既定が "automatic" になり、react/jsx-runtime から読み込んだ関数の呼び出しに変換する。
さらに development の既定が環境に従うようになり、NODE_ENV などを指定しないと開発用の react/jsx-dev-runtime を使う。

v7 では

$ cat babel.config.json
{
  "presets": ["@babel/preset-react"]
}
$ npx babel src/app.jsx
export const App = () => /*#__PURE__*/React.createElement("h1", null, "Hello");
$ echo $?
0

v8 に上げると

$ npx babel src/app.jsx
import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
export const App = () => /*#__PURE__*/_jsxDEV("h1", {
  children: "Hello"
}, void 0, false);
$ echo $?
0

v8 で直すと

$ env NODE_ENV=production npx babel src/app.jsx
import { jsx as _jsx } from "react/jsx-runtime";
export const App = () => /*#__PURE__*/_jsx("h1", {
  children: "Hello"
});
$ echo $?
0
$ cat babel.config.json
{
  "presets": [["@babel/preset-react", { "runtime": "classic" }]]
}
$ npx babel src/app.jsx
export const App = () => /*#__PURE__*/React.createElement("h1", null, "Hello");
$ echo $?
0

NODE_ENV=production を付けて走らせると、本番用の react/jsx-runtime を使う出力になった。
本番のビルドで NODE_ENV を付けていないなら、開発用の出力が配られることになる。
NODE_ENV を変えずに済ませたいなら、preset-react に development: false を書く。

7 と同じ React.createElement に戻したいときは、runtime: "classic" を書く。
react/jsx-runtime の無い古い React を使っているときと、Preact などで pragma を指定しているときがこれに当たる。
React の公式ブログによると、react/jsx-runtime は React 17 で入り、16.14.0・15.7.0・0.14.10 にも足された。

automatic のまま使うなら、ファイルの先頭の import React from "react" は要らなくなる。

検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-08 検証(ほか 14 件)
検証日
実行環境
node:24 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 24.22.2
  • npm 11.19.1
  • Python 3.11.2
  • Git 2.39.5
  • @babel/core(旧) 7.29.13
  • @babel/core(新) 8.1.0
  • @babel/cli(旧) 7.29.7
  • @babel/cli(新) 8.1.0
  • @babel/preset-env(旧) 7.29.7
  • @babel/preset-env(新) 8.0.7
  • @babel/preset-react(旧) 7.29.7
  • @babel/preset-react(新) 8.0.1
  • @babel/preset-typescript(旧) 7.29.7
  • @babel/preset-typescript(新) 8.0.7
  • babel-plugin-polyfill-corejs3 1.0.0
  • core-js 3.51.0
  • @babel/plugin-proposal-class-properties 7.18.6

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