6
1

More than 3 years have passed since last update.

Yes/Noの対話形式でnodejs使う

Last updated at Posted at 2020-06-10

nodejs使うときに対話形式で確認するやり方を書きます。

インストール

モジュールをインストール

$ npm i --save readline-sync colors

使い方

keyInYNでy/nで答えて、後続の処理を続けるか、やめるか判断する
(colorsはconsoleに色をつけるだけ)

test.js
const readlineSync = require('readline-sync')
const colors = require('colors/safe')

if (readlineSync.keyInYN('本当に実行しますか?')) {
  console.log(colors.green('実行します。'))
} else {
  console.log(colors.yellow('キャンセルしました。'))
  process.exit()
}

console.log('後続の処理')

実行

test.jsを実行します。

$ node test

出力結果

実行します。
後続の処理

or

キャンセルしました。

参考文献

この記事は以下の情報を参考にして執筆しました。

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1