LoginSignup
12
7

More than 5 years have passed since last update.

パッケージ名とコマンドが異なるモジュールを npx で実行する

Posted at

はじめに

npx は便利ですね。
npm install -g foobar でローカルにインストールしなくても、npx foobar だけで実行できます。

npx にちょっとはまって、ちょっとだけ調べたので、そのメモです。

間違ったやり方

例えば express-generator。express のテンプレートを作れるコマンドです。

インストールは、npm install -g express-generator ですが、実際使うときのコマンドは、express なので、上のノリで npx express とやると、

$ npx express -h
npx: installed 48 in 10.238s
command not found: express

となります。

どうやら、npx foobar はパッケージ名とコマンドが一致する場合の特別なケースでのやり方のようです。

正しいやり方

一般的にコマンドとパッケージ名は別物なので、そういうコマンドを実行する時は、-p オプションでパッケージ名を、-c オプションでコマンドを、それぞれ明示的に指定して、npx を実行します。

$ npx -p express-generator -c "express -h"
npx: installed 10 in 5.393s

  Usage: express [options] [dir]


  Options:

        --version        output the version number
    -e, --ejs            add ejs engine support
    (以下略)

動いたようです。

以上、公式の README の先頭に書いてある話でした。
https://github.com/zkat/npx#readme

余談

npx は便利ですが、babel みたいに開発中に何度も実行するコマンドまで、npx で実行すると、当たり前ですけど効率は落ちまくりますw
テンプレート作成のようなコマンドは、npx 、その他は npm i --save-dev、みたいに使い分けが必要そうです。

12
7
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
12
7