I am new to NodeJs. Consider I am having three different js files in my folder. When I want to run my node js file, I want to type "node file1.js" in the command prompt. Again when I want to run another js file I have to stop the previously running js file and I must give "node file2.js". Or I have to open new command prompt and run the second js file. Likewise I have to do it for the third file also. This irritated me a lot. Is there any way to run all the js files at sametime without opening new command prompts. Please help me to solve this issue.
To avoid using different command prompts, you may use some "launcher", such as forever
or pm2
.
With these, you can run your JS files like this:
forever start myJsFile1.js
forever start myJsFile2.js
forever start myJsFile3.js
and you can view some stats about your files with forever list
.
Documentations:
Another way to get your prompt clean is to put the process in background right after you start it. It will avoid you to start new prompts.
node myJsFile1.js &
node myJsFile2.js &
node myJsFile2.js &
But, if you really want to launch your files at the same time exactly, I don't understand how your program works. Because, you can just start one file and require the others or use them in your main file in some way.
Write a bash(one of shell scripts) called filename.sh
to do these things. If you work on Windows, you can write a filename.bat
by Batch
. Here is an example of bash script for you.
#!/bin/bash
for d in * ; do
if [[ $d == *\.js ]]
then
node "$d" &
fi
done
If you run command sh filename.sh
to execute the code above, it will run all the js file in current directory. And you can make some filter rule in if
block to skip the file you do not want to run. I think you can do it in this way.