npm run
套件與執行環境
用法
npm run <script> [-- <args>]執行 package.json 裡 scripts 段落中的命令;要把參數傳給指令碼,前面得先寫一個單獨的 --,而只有 start 和 test 可以不寫 run。
常用選項
| 選項 | 含義 |
|---|---|
| -- | everything after it goes to the script, not to npm |
| --if-present | exit quietly when the script is missing |
| -w <workspace> | run it in one workspace |
| --workspaces | run it in every workspace |
| -s | hide npm own output |
範例
npm run buildruns the build script from package.json
npm run test -- --watchpasses --watch to the test runner, not to npm
npm runwith no name, lists the scripts that exist
問題永遠是什麼裝到哪裡:裝進這個專案,還是裝到整台機器上。
怎麼看
- 方括號 [ ] 表示這部分可以不寫。
- 省略號 … 表示可以寫多個。
- 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。
常見問題
Q. npm run 是做什麼的?
執行 package.json 裡 scripts 段落中的命令;要把參數傳給指令碼,前面得先寫一個單獨的 --,而只有 start 和 test 可以不寫 run。
Q. 怎麼寫?
npm run <script> [-- <args>] —— 方括號表示可以省略的部分。
Q. 值得記的選項有幾個?
這裡列了 5 個,完整列表在 man npm。這條命令屬於套件與執行環境。
相關命令
man 手冊: man npm-run