是否可以在Express应用程序中使用一个npm脚本启动nodemon和浏览器同步?

问题描述

在我的Express应用中,我同时使用nodemonbrowser-sync。我的package.json中有这些npm脚本:

  "scripts": {
    "start": "node  ./bin/www","start:nodemon": "nodemon ./bin/www","start:debug": "SET DEBUG=img:* & npm run start:nodemon","start:browser-sync": " browser-sync start --proxy 'localhost:3000' --files 'public,views'","test": "npm run start:debug & npm run start:browser-sync"
  }

当前,我打开两个cmd窗口,在第一个窗口中,我运行start:debug,在另一个窗口中,我运行start:browser-sync。一切正常。

我以为我可以合并这些脚本并像在test脚本中那样运行它们,但是,这种方式行不通。看起来它开始nodemon并忽略了browser-sync。因此,我可以以某种方式与一个npm脚本一起启动这两个脚本,还是从技术上讲是不可能的,并且我必须运行两个cmds才能使其工作?谢谢。

解决方法

这是因为在Bash中,命令之间的gcloud auth print-identity-token意味着“在后台执行第一个命令,而在前景中执行第二个命令”,而在Windows cmd中,这意味着“在第一个命令完成后执行第二个命令”。 / p>

要以与平台无关的方式并行运行命令,可以使用例如npm-run-all

&

或者您可以尝试run-z

{
  "scripts": {
    "start": "node  ./bin/www","start:nodemon": "nodemon ./bin/www","start:debug": "SET DEBUG=img:* & npm run start:nodemon","start:browser-sync": " browser-sync start --proxy 'localhost:3000' --files 'public,views'","test": "run-p start:debug start:browser-sync"
  }
}

后者无需安装即可使用。刚运行

{
  "scripts": {
    "start": "node  ./bin/www","test": "run-z start:debug,start:browser-sync"
  }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...