Node.jsのバージョンによってJSON.parse()でエラーが発生したときのメッセージが異なります(parseメソッドだけではありませんが)。
正確にはNode.jsのバージョンではなく、Node.jsに梱包されているV8エンジンのバージョンによります。
この記事ではそれぞれのNode.jsのバージョン、V8エンジンのバージョンとメッセージを確認していきます。
実行したJavaScriptのコードは以下です。
JSON.parse('a');
Node.jsに含まれているV8エンジンのバージョンは、以下のコマンドで確認できます。
$ node -p process.versions.v8
https://nodejs.org/docs/latest-v24.x/api/process.html#processversions
バージョンごとのメッセージ
v16
$ node -v
v16.20.2
$ node index.js
undefined:1
a
^
SyntaxError: Unexpected token a in JSON at position 0
at JSON.parse (<anonymous>)
at Object.<anonymous> (<my-directory>/index.js:1:6)
at Module._compile (node:internal/modules/cjs/loader:1198:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
at Module.load (node:internal/modules/cjs/loader:1076:32)
at Function.Module._load (node:internal/modules/cjs/loader:911:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47
$ node -p process.versions.v8
9.4.146.26-node.26
v18
$ node -v
v18.20.8
$ node index.js
undefined:1
a
^
SyntaxError: Unexpected token a in JSON at position 0
at JSON.parse (<anonymous>)
at Object.<anonymous> (<my-directory>/index.js:1:6)
at Module._compile (node:internal/modules/cjs/loader:1364:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)
at Module.load (node:internal/modules/cjs/loader:1203:32)
at Module._load (node:internal/modules/cjs/loader:1019:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:128:12)
at node:internal/main/run_main_module:28:49
$ node -p process.versions.v8
10.2.154.26-node.3
v20
$ node -v
v20.20.0
<anonymous_script>:1
a
^
SyntaxError: Unexpected token 'a', "a" is not valid JSON
at JSON.parse (<anonymous>)
at Object.<anonymous> (<my-directory>/index.js:1:6)
at Module._compile (node:internal/modules/cjs/loader:1521:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1623:10)
at Module.load (node:internal/modules/cjs/loader:1266:32)
at Module._load (node:internal/modules/cjs/loader:1091:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:164:12)
at node:internal/main/run_main_module:28:49
$ node -p process.versions.v8
11.3.244.8-node.3
v22
$ node -v
v22.22.0
$ node index.js
<anonymous_script>:1
a
^
SyntaxError: Unexpected token 'a', "a" is not valid JSON
at JSON.parse (<anonymous>)
at Object.<anonymous> (<my-directory>/index.js:1:6)
at Module._compile (node:internal/modules/cjs/loader:1706:14)
at Object..js (node:internal/modules/cjs/loader:1839:10)
at Module.load (node:internal/modules/cjs/loader:1441:32)
at Function._load (node:internal/modules/cjs/loader:1263:12)
at TracingChannel.traceSync (node:diagnostics_channel:328:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:237:24)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
at node:internal/main/run_main_module:36:49
$ node -p process.versions.v8
12.4.254.21-node.3
v24
$ node -v
v24.13.1
$ node index.js
<anonymous_script>:1
a
^
SyntaxError: Unexpected token 'a', "a" is not valid JSON
at JSON.parse (<anonymous>)
at Object.<anonymous> (<my-directory>/index.js:1:6)
at Module._compile (node:internal/modules/cjs/loader:1804:14)
at Object..js (node:internal/modules/cjs/loader:1936:10)
at Module.load (node:internal/modules/cjs/loader:1525:32)
at Module._load (node:internal/modules/cjs/loader:1327:12)
at TracingChannel.traceSync (node:diagnostics_channel:328:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:245:24)
at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
at node:internal/main/run_main_module:33:47
$ node -p process.versions.v8
13.6.233.17-node.40
まとめ
バージョンとメッセージは以下の表の通りになりました。
| Node.js |
V8 |
メッセージ |
| v16.20.2 |
9.4.146.26-node.26 |
Unexpected token a in JSON at position 0 |
| v18.20.8 |
10.2.154.26-node.3 |
Unexpected token a in JSON at position 0 |
| v20.20.0 |
11.3.244.8-node.3 |
Unexpected token 'a', "a" is not valid JSON |
| v22.22.0 |
12.4.254.21-node.3 |
Unexpected token 'a', "a" is not valid JSON |
| v24.13.1 |
13.6.233.17-node.40 |
Unexpected token 'a', "a" is not valid JSON |
現時点でのLTSの最新バージョンしか調べていませんが、v20からメッセージが変わって、わかりやすくなっています。
エラーメッセージで分岐を作るようなことはしないようにしましょう。