简体   繁体   English

package.json 中的多个命令

[英]Multiple commands in package.json

This command: "start": "node server/server.js" starts my server, but before running this I also want a command to run automatically: 'webpack' .这个命令: "start": "node server/server.js"启动我的服务器,但在运行它之前,我还希望一个命令自动运行: 'webpack'

I want to build a script that can be run with npm run someCommand - it should first run webpack in the terminal, followed by node server/server.js .我想构建一个可以使用npm run someCommand的脚本 - 它应该首先在终端中运行webpack ,然后是node server/server.js

(I know how configure this with gulp, but I don't want to use it) (我知道如何使用 gulp 进行配置,但我不想使用它)

If I understood you correctly, you want firstly run webpack and after compile run nodejs.如果我理解正确,您首先要运行 webpack,然后编译运行 nodejs。 Maybe try this:也许试试这个:

"start": "webpack && node server/server.js"

The following should work:以下应该有效:

"start": "webpack && node server/server.js"

Though, for readability (and especially if you plan on adding additional tasks in the future), you may want to consider creating separate entries for each task and then calling each of those from start .但是,为了可读性(特别是如果您计划在将来添加其他任务),您可能需要考虑为每个任务创建单独的条目,然后从start调用每个条目。 Something like:就像是:

{
    "init-assets": "webpack",
    "init-server": "node server/server.js",
    "start": "npm run init-assets && npm run init-server"
}

You can also chain commands like this:您还可以像这样链接命令:

"scripts": {
    "clean": "npm cache clean --force",
    "clean:complete": "npm run clean && npm uninstall -g @angular/cli && rmdir /Q /S node_modules",
    "clean:complete:install": "npm run clean:complete && npm i -g @angular/cli && npm i && npm install --save-dev @angular/cli@latest"
}

Better understand the && operator更好地理解&&运算符

In my case the && didn't worked well because one of my commands exited with 1 (error) or 0 (success) depending on the situation AND the && chaining operator works only if the previous command succeeds .在我的情况下, &&效果不佳,因为我的一个命令根据情况以 1(错误)或 0(成功)退出,并且&&链接运算符仅在前一个命令成功时才有效

So, to add to other answers here are the options you get to separate commands:因此,为了添加其他答案,您可以使用以下选项来分隔命令:

  • && run second command only if first succeeds &&仅在第一个成功时运行第二个命令
  • || run second command only if first fails仅在第一个失败时运行第二个命令

So if you want the second command to run whatever the first has outputted the best way is to do something like (command1 && command2) || command 2因此,如果您希望第二个命令运行第一个输出的任何命令,最好的方法是执行类似(command1 && command2) || command 2之类的操作。 (command1 && command2) || command 2

OS specific chaining operators操作系统特定的链接运算符

Other options are different in unix (linux, macos) and windows environnement其他选项在 unix(linux、macos)和 windows 环境中有所不同

  • ; UNIX run the second command whatever the first has outputted UNIX 运行第二个命令,无论第一个命令输出什么
  • ; WIN separate command arguments WIN 单独的命令参数
  • & UNIX run first command in the background parallel to the second one & UNIX 在后台并行运行第一个命令与第二个命令
  • & WIN run the second command whatever the first has outputted & WIN 无论第一个输出的是什么,都运行第二个命令

All chaining operators for windows here and for unix here 此处为 windows 和此处为 unix 的所有链接运算符

Also, along with the accepted answer and @pdoherty926's answer, in case you want to have run two command prompts, you can add "start" before each command:此外,除了接受的答案和@pdoherty926 的答案,如果您想运行两个命令提示符,您可以在每个命令之前添加“start”:

{
    "init-assets": "webpack",
    "init-server": "node server/server.js",
    "start": "start npm run init-assets && start npm run init-server"
}

You can use a tool like script-launcher to extend the features of you package.json file. 您可以使用脚本启动器之类的工具来扩展package.json文件的功能。

With script-launcher you can use arrays as scripts and reference another script with different arguments, and many more. 使用脚本启动器,您可以将数组用作脚本,并引用具有不同参数的其他脚本,等等。

Example using script arrays 使用脚本数组的示例

{
  "scripts": {
    "init": [
      "webpack",
      "node server/server.js"
    ]
  }
}

Use the examples from the Table of Contents on how to implement this. 使用目录中的示例来了解如何实现此目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM