npx
套件與執行環境
用法
npx [-y] <package>[@version] [args...]執行某個套件的執行檔,優先用 node_modules/.bin,找不到就臨時下載;打錯一個字就可能拉來陌生的套件,加 --no 可以只用本機的。
常用選項
| 選項 | 含義 |
|---|---|
| -y | install without asking first |
| --no | fail 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 --noEmitruns the typescript in node_modules, no global install
npx -y create-next-app@latest my-appfetches 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