【Node.js】コマンド ライン引数を取得する方法

// test.js
const args = process.argv.slice(2);
console.log(args);
  • 実行コマンド
$ node test.js foo bar
["foo", "bar"]

argvの格納値

インデックス 格納値
0 node実行ファイルのフルパス
1 スクリプトファイルのフルパス
2以降 コマンドライン引数