admin管理员组文章数量:1353311
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 ?
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 ?
Share Improve this question edited Oct 31, 2019 at 7:11 user12290051 asked Oct 31, 2019 at 7:06 user12290051user12290051 191 gold badge1 silver badge4 bronze badges 5- are you exiting script1.js with non zero exit code ? – Golak Sarangi Commented Oct 31, 2019 at 7:19
- No actually my script files are services listening to specific code – user12290051 Commented Oct 31, 2019 at 7:21
- single ampersand is used for running scripts in background – Golak Sarangi 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 mand I have mentioned in the answer below – Golak Sarangi Commented Oct 31, 2019 at 7:23
-
simply use
&
instead of&&
as explained here :itnext.io/… worked for me on mac (node 14.7) – Hacke Commented Aug 16, 2021 at 18:55
4 Answers
Reset to default 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 mand 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 mands 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
Use the pipe operator (windows). node service1.js | node service2.js
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
本文标签: javascriptHow to run multiple js file using node js command lineStack Overflow
版权声明:本文标题:javascript - How to run multiple js file using node js command line? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743905124a2559378.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论