npx

パッケージと実行環境

書き方

npx [-y] <package>[@version] [args...]

パッケージの実行ファイルを呼び出します。node_modules/.binを先に見て、無ければ一時的に取ってくるので、打ち間違いで別のパッケージを走らせないよう--noを付けます。

よく使うオプション

オプション意味
-yinstall without asking first
--nofail instead of downloading anything
-p <pkg>make a package available, then run a command from it
-c "<cmd>"run the command through a shell with the bin path set
--package <pkg>the long form of -p

npx tsc --noEmit

runs the typescript in node_modules, no global install

npx -y create-next-app@latest my-app

fetches a scaffolder for one use and forgets it

npx --no eslint .

uses the local eslint or fails, instead of quietly downloading one

何をどこに入れるかが問題です。このプロジェクトの中か、マシン全体かを先に決めてください。

読み方

  • 角括弧 [ ] は省いてよい部分です。
  • 三点 … は複数並べられるという意味です。
  • オプションは大文字小文字を区別します — -r と -R が別物のコマンドもあります。

よくある質問

Q. npx は何をしますか。

パッケージの実行ファイルを呼び出します。node_modules/.binを先に見て、無ければ一時的に取ってくるので、打ち間違いで別のパッケージを走らせないよう--noを付けます。

Q. どう打ちますか。

npx [-y] <package>[@version] [args...] — 角括弧は省いてよい部分です。

Q. よく使うオプションはいくつですか。

ここにまとめたのは5個です。全部は man npx にあります。このコマンドはパッケージと実行環境の仲間です。

関連するコマンド

man ページ: man npx