简体   繁体   English

在 npm 脚本中运行 2 个命令(nodemon && sass --watch)

[英]Running 2 commands in npm script(nodemon && sass --watch)

I have a package.json file looks like this.我有一个 package.json 文件,如下所示。


"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/app.js",
    "dev": "nodemon src/app.js -e js,hbs ",
    "scss": "sass --watch public/scss:public/css",
    "both": "nodemon src/app.js -e js,hbs && sass --watch public/scss:public/css",
    "both2" : "npm run dev && npm run scss"
  },

I wonder why I cannot run these 2 commands:我想知道为什么我不能运行这两个命令:

"both": "nodemon src/app.js -e js,hbs && sass --watch public/scss:public/css" “两者”:“nodemon src/app.js -e js,hbs && sass --watch public/scss:public/css”

by经过

npm run both

When I try to run it, only the first command is working.当我尝试运行它时,只有第一个命令有效。

Github repository is below, just in case you need some testing. Github 存储库在下面,以防万一您需要一些测试。

https://github.com/tuanphanfi/weather-app-nodejs/ https://github.com/tuanphanfi/weather-app-nodejs/

Use a package called concurrently . 使用一个并发的包。

npm install concurrently

Then you can make a script called both 然后,你可以做一个调用的脚本both

"both": "concurrently \"nodemon src/app.js -e js,hbs\" \"sass --watch public/scss:public/css\""

See javascript - How can I run multiple npm scripts in parallel? 请参阅javascript-如何并行运行多个npm脚本?

On UNIX-like systems you can just use & instead of && to chain your commands.在类 UNIX 系统上,您可以只使用&而不是&&来链接您的命令。 & is used for parallel script execution whereas && causes a sequential execution , meaning the 2nd one starts only when the 1st has finished successfully. &用于并行脚本执行,而&&导致顺序执行,这意味着第二个仅在第一个成功完成时才开始。

cmd1 &  cmd2 & ...     # parallel 
cmd1 && cmd2 && ...    # sequential 

Whereas on Windows-Systems prepending each of your commands with start and chaining them with && seems to do the trick...而在 Windows 系统上,在每个命令前加上start并用&&链接它们似乎可以解决问题……

start cmd1 && start cmd2 && start ... 

For cross-platform solutions, see fi either concurrently , like Namko suggests, or npm-run-all .对于跨平台解决方案,请参阅 fi concurrently ,如 Namko 建议,或npm-run-all Those solve other problems as well, that might or might not occur when using the above platform-specific solutions (problems like weired/mixed terminal-outputs, multiple terminals/windows, bad control over process/thread-termination etc.)...这些也解决了其他问题,这些问题在使用上述特定于平台的解决方案时可能会或可能不会发生(诸如奇怪/混合终端输出、多个终端/窗口、对进程/线程终止的不良控制等问题)...

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

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