0

I made a nodeJS script that has a parameter.

At this state, i can pass arguments to npm start and it will fire up my script with it, and all works perfectly.

Now I have to pass multiple arguments to npm start and it will fire up as many scripts needed. ( one per argument )

But I really have no idea how to do that except to make the other script that accepts all arguments and launch etc... but I don't want this solution. So if some of you have the answer it will be nice!

Thanks.

Vineet Jain
  • 1,515
  • 4
  • 21
  • 31
Psyko460
  • 1
  • 6
  • Do you need multiple Node processes running, or are you okay with all the arguments being handled in a single Node process? – TKoL Sep 20 '17 at 08:06
  • In a single node process it's perfect but if it as to be multiple node processes running it's fully ok though – Psyko460 Sep 20 '17 at 08:08

1 Answers1

0

Given that you're fine with the arguments being handled in a single process, the answer is simple. Whatever way your current program runs on a single argument, make that into a function and pass into it your multiple arguments, separately.

According to this answer, you can access the arguments array via process.argv.

So if your program used to do console.log(process.argv[0]), what you would do now is make your previous program into a function, function run(arg) { console.log(arg) }, and then run that function on each argument, process.argv.forEach(run)

TKoL
  • 13,158
  • 3
  • 39
  • 73
  • Yes that can possibly be the solution. So the whole thing will work under one process. Do you think this solution will be less effective comparing to one process for each arg ? – Psyko460 Sep 20 '17 at 09:33
  • Depends on your use case. If you can't name a good reason why you'd have to run multiple processes simultaneously instead of doing it like this, then you probably don't need to. Sure, it would be marginally faster to run multiple processes simultaneously instead, but a lot of the time you genuinely don't need it to be marginally faster. Do you have a reason why you'd want it to take 1 second instead of 2? – TKoL Sep 20 '17 at 09:46
  • Also, I don't know what your script is going to be doing, but if running multiple processes simultaneously has a risk of trying to access shared resources, running them this way could be much safer and smarter. – TKoL Sep 20 '17 at 09:48
  • I think the reason why is not enough good to go for the multiple processes. So i will follow your advice and going for your solution. Thanks mate ! – Psyko460 Sep 20 '17 at 09:50