简体   繁体   English

如何使用 node js 命令行运行多个 js 文件?

[英]How to run multiple js file using node js command line?

I want to start 4 script using node js.我想使用节点 js 启动 4 个脚本。

myapp
  -script1.js
  -script2.js
  -script3.js
  -app.js
  -package.json
  ....
  ....

I tried running it using below我尝试使用下面运行它

node script1.js && node script2.js && node script3.js && node app.js

node script1.js & node script2.js & node script3.js & node app.js

But its not starting all script it only start script1.js .但它并没有启动所有脚本,它只启动script1.js

How to do it?怎么做?

$ node script-1.js  && node script-2.js && node script-3.js && node app.js
I am script-1
I am script-2
I am script-3
I am app.js

It is working.这是工作。

Maybe your script1.js is blocking those other scripts that are on the queue.也许您的script1.js正在阻止队列中的其他脚本。

Node runs it in a synchronous way. Node 以同步方式运行它。

If you want to run those scripts in parallel.如果您想并行运行这些脚本。

You can use npm package called concurrently您可以同时调用 npm package

In command line.在命令行中。

$ concurrently "node script-1.js" "node script-2.js" "node script-3.js" "node app.js"
[3] I am app.js
[2] I am script-3
[0] I am script-1
[1] I am script-2
[2] node script-3.js exited with code 0
[3] node app.js exited with code 0
[0] node script-1.js exited with code 0
[1] node script-2.js exited with code 0
Done in 1.07s.

Or you can put it on your package.json scripts .或者你可以把它放在你的package.json 脚本上。

"scripts": {
    "start": "concurrently \"node script-1.js\" \"node script-2.js\" \"node script-3.js\" \"node app.js\""
}

It will run multiple commands concurrently / in asynchronous way.它将同时/以异步方式运行多个命令。

Hope it helps.希望能帮助到你。 :) :)

Try this.尝试这个。 it will execute all scripts irrespective of the exit code of the previous script无论前一个脚本的退出代码如何,它将执行所有脚本

node script1.js; node script2.js; node script3.js; node app.js

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

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