为什么在构建 angular 应用程序(npm 运行 构建)时需要添加 'run' 参数?

Why do I need to add the 'run' argument when building an angular app (npm run build)?

我只是好奇。这是 package.json 文件的一部分

"start": "ng serve -o",
"build": "ng build",
"build-prod": "ng build --prod",
"test": "ng test",

我只是好奇。为什么写 npm startnpm test 有效。但是,npm build 或 npm build-prod 给我以下错误 build npm build called with no arguments. Did you mean to 'npm run-script build'? 为什么构建时需要添加 运行npm run buildnpm run build-prod

感谢您的帮助

npm-run 文档的 “描述” 部分指出:

"run[-script] is used by the test, start, restart, and stop commands, but can be called directly, as well."

注: 粗体强调是我加的

本质上,npm 考虑在 package.jsonscripts 部分中定义的任何脚本; teststartrestartstop,成为 “生命周期” 脚本。这四个 “生命周期” 脚本中的任何一个都可以在 没有 run 关键字的情况下被调用。

上述四个 npm 脚本名称(即 "lifecycle" 脚本)中的每一个都有一个专用的 ClI Command, namely npm test, npm start, npm restart, and npm stop,用于调用它们。

作为示例,请考虑在 package.json 中定义的以下人为设计的 "scripts" 部分:

"scripts": {
  "start": "echo \"starting...\"",
  "restart": "echo \"restarting...\"",
  "stop": "echo \"stopping...\"",
  "test": "echo \"testing...\"",

  "foo": "echo \"foo...\""
},
  • 名为 foo 的脚本是唯一必须使用 run 关键字调用的脚本,即 npm run foo.

  • 与其他的不同,即:startrestartstoptest,它们可以在没有 run关键字;即 npm startnpm stop 等等。

    旁注: startrestartstoptest 可以调用也使用 run 关键字。即 npm run startnpm run stop 等等。

文档还说明了以下关于 npm run 的内容:

"If no "command" is provided, it will list the available scripts [...] When the scripts in the package are printed out, they're separated into lifecycle (test, start, restart) and directly-run scripts."

鉴于你的问题中显示的 package.json,如果你 cd 到你的项目目录并且 运行 npm run您会看到 starttest 脚本都列在 “生命周期” 部分下。