在我的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才能工作?谢谢。
发布于 2020-10-26 13:56:37
这是因为在Bash中,命令之间的&意味着“在后台执行第一个命令,而在前台执行第二个命令”,而在Windows cmd中,这意味着“在第一个命令完成后执行第二个命令”。
要以独立于平台的方式并行运行命令,您可以使用例如npm-run-all
{
"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"
}
}或者你可以尝试一下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-z start:debug,start:browser-sync"
}
}后者无需安装即可使用。快跑吧
npx run-z start:debug,start:browser-synchttps://stackoverflow.com/questions/64526949
复制相似问题