问题描述
在我的Express应用中,我同时使用nodemon
和browser-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"
}
}