简体   繁体   中英

How to run multiple js file using node js command line?

I want to start 4 script using node js.

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 .

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.

Node runs it in a synchronous way.

If you want to run those scripts in parallel.

You can use npm package called concurrently

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 .

"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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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