LoginSignup
6
9

More than 5 years have passed since last update.

Node.jsでターミナル用コマンドユーティリティ制作 はじめの一歩

Posted at

はじめに

terminalでyeomanやgruntなどを叩きますが、イマイチ仕組みを理解していませんでした。

node.jsでターミナル上のコマンドってどうしたら作れるんだろうとか自分でも作りたいものあるなということで、調べてみました。

helloJsコマンドを作る

参考ブログ
http://cruft.io/posts/node-command-line-utilities/

このブログでひと通り理解できました。。

実行するファイルを作成します。

index.js
#!/usr/bin/env node

console.log("HellowWorld.");

これだけでいいそうです。。

#!/usr/bin/env node はファイル実行時にどのインタプリタを使うのか決定します。Node.jsを使用したいので#!/usr/bin/env nodeとします。

後はpacakge.jsonを作成します。

package.json
{
    "name": "hello-world",
    "version": "0.0.1",

    "description": "A simple command-line tool for helloworld",
    "author": "kenjis-special",
    "engines": {
      "node": ">=0.10"
    },
    "dependencies": {
    },
    "bin": {
      "hello-world": "index.js"
    }
}

package.jsonのbinに実行コマンドと参照ファイルを設定します。

以上で終了です。

npm install -g

nodeモジュールとしてインストールされます。

Screenshot 2015-01-15 16.41.16.png

hello worldコマンド完成です!

6
9
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
9