Node.jsでコマンドラインツールを作成

Kaoru Mori
Sep 5, 2018 · 2 min read

以下のエントリーを参考にしてTypeScriptでコマンドラインツールを作成しようとしました。

しかし、利用するライブラリとの相性もあったようでトラブルがあり、今回はJavascriptを使うことにしました。

npmコマンドで初期化します。

$ npm init

src/main.js を作成します。

console.log('Hello');

bin/hello.js を作成します。

#!/usr/bin/env node
require('../src/main.js');

package.jsonにコマンドを登録します。

"bin": {
"hello": "./bin/hello.js"
},

npmでリンクを登録します。

$ npm link

これでコマンドが利用できるようになりました。

$ hello
Hello
Kaoru Mori

Written by

ねこぱんち研究所 主任研究員, kaoru.mori@gmail.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade