首页·终端命令

npm run

包与运行时

用法

npm run <script> [-- <args>]

运行 package.json 里 scripts 段落中的命令;要把参数传给脚本,前面得先写一个单独的 --,而只有 start 和 test 可以不写 run。

常用选项

选项含义
--everything after it goes to the script, not to npm
--if-presentexit quietly when the script is missing
-w <workspace>run it in one workspace
--workspacesrun it in every workspace
-shide npm own output

示例

npm run build

runs the build script from package.json

npm run test -- --watch

passes --watch to the test runner, not to npm

npm run

with 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