Node.js のバージョン管理に fnm を利用しているので、VSCode で利用する方法をまとめておく。
目次
0. Node.js のバージョンを確認しておく
VSCode でターミナルを開く(Ctrl + J か Ctrl + @ )。
node -v
で Node.js のバージョンを確認。

1. launch.json を作成する
VSCodeのサイドメニュー [実行とデバッグ] をクリックし、「launch.json ファイルを作成します」をクリック。

ワークスペース下にある .vscode
に launch.json
が作られる。
2. launch.json に設定を書き込む
launch.json に以下のように書く。
"runtimeVersion"
部分には利用したい Node.js のバージョンを書く。
{
// IntelliSense を使用して利用可能な属性を学べます。
// 既存の属性の説明をホバーして表示します。
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"runtimeVersion": "14.19.1",
"request": "launch",
"name": "Node: Current File (with FNM)",
"skipFiles": [
"<node_internals>/**"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"program": "${file}",
"runtimeExecutable": "fnm",
"runtimeArgs": [
"exec",
"--using-file",
"--",
"node"
],
"restart": true
}
]
}
3. 実行する
VSCodeを再起動し、VSCodeのサイドメニュー [実行とデバッグ] をクリック。先程作ったものがプルダウンメニューにあるので選択し、三角部分を押して実行。

VSCodeの下部の色がオレンジになり実行してる内容が表示される。

4. Node.js のバージョンを確認する
node -v
で Node.js のバージョンを確認。

設定したバージョンになっていれば OK。
5. 止めるには
VSCodeのソースコード編集画面右上あたりに表示されている下記画像の、四角ボタンを押す。
