LoginSignup
4
5

More than 5 years have passed since last update.

nodeでcliツールを作る

Last updated at Posted at 2019-02-09

ターミナルで

my-cli --env=production

みたいな感じで起動できる対話型のcliツールを作るやり方です。

対話型cli

enquirerというnpmを使えば対話型のcliツールは簡単に作れます。

標準入力からもらう引数

標準入力から引数をもらうにはyargsというnpmを使えば簡単にできます。

独自コマンド

node index.jsみたいに起動させるのではなく、my-cliのような独自コマンドでプログラムを動かすにはpackage.jsonのbinを使います。
package.jsonに以下のように書いて

  "bin": {
    "my-cli": "./index.js"
  }

index.jsの先頭に

#!/usr/bin/env node

を書いて、

npm install -g

を実行します。

これでターミナルでmy-cliと実行することで./index.jsが動くようになります。

4
5
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
4
5