0

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 ?

5
  • are you exiting script1.js with non zero exit code ? Commented Oct 31, 2019 at 7:19
  • No actually my script files are services listening to specific code Commented Oct 31, 2019 at 7:21
  • single ampersand is used for running scripts in background Commented Oct 31, 2019 at 7:22
  • It's difficult to answer without seeing the actual script. If all of them are independent scripts then please use the command I have mentioned in the answer below Commented Oct 31, 2019 at 7:23
  • simply use & instead of && as explained here :itnext.io/… worked for me on mac (node 14.7) Commented Aug 16, 2021 at 18:55

4 Answers 4

5
$ 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. :)

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

I tried it give Error: Cannot find module 'D:\myapp\script1.js;'
0

Use the pipe operator (windows). node service1.js | node service2.js

Comments

0

you can use npm concurrently.

first install npm install -g concurrently 

Then:

Run npm init to the parent directory of both of the projects.
the parent directory's package.json file will look like this.

{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "concurrently \"npm run develop --prefix project1\" \"npm run develop --prefix project2\""
  },
  "author": "",
  "license": "ISC"
}

Then:

 write inside the "start" like above . just change the folder name of project1 and project2 with your projects name.

Then:

npm start


 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.