2

How would I do something similar to a command line command (CMD) node runthis.js from another javascript file? I also need to run multiple .js files at the same time where they will have a loop that will wait for a certain condition to be met.

I need to check if a condition is met every second and then when a condition is met a function will be executed. The condition is the current time against a time specified by the user.

4
  • It sounds like you are looking for child_process.fork. Commented Jul 27, 2018 at 16:37
  • Please explain in more details what you are trying to achieve. Also, what have you tried? Commented Jul 27, 2018 at 16:39
  • There are a couple warning signs here. First off, you don't loop waiting for a condition to be met in node.js. It's single- threaded, event driven so you don't loop waiting for something external to happen and change something in the loop. Commented Jul 27, 2018 at 16:40
  • I need to check if a condition is met every second and then when a condition is met a function will be executed. Commented Jul 27, 2018 at 16:41

2 Answers 2

2

You can load a Javascript file into the current node.js runtime environment with require('./runthis.js').

Or, you can use the child_process module to run another node.js process that runs your script and you can communicate with that other process any number of ways (stdio, stdout, message sending, other interprocess communication, etc...). There are plenty of code examples in that documentation to show you how starting another node.js process works.

The part about "looping until a condition is met" sounds like probably the wrong design for a single-thread, event-driven node.js environment, but without knowing a lot more specifically about what you're really trying to do, we can't really offer a specific solution.

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

5 Comments

It needs to check the current time and when the time occurs it has to execute a function.
@henry.miller - If you have a specific question, then edit your question and ask the specific question. I've answered the question you actually wrote above which is how to execute another script. It gets so frustrating here when you have a specific question, but you don't actually describe or ask it. Instead, people ask some general question. I answer the general question and then you say that isn't enough. I can ONLY answer what you actually ask. And, you need to edit your question to say what you want us to answer. On stack overflow here, we don't just take new questions in comments.
@henry.miller - I see you're probably new here so I don't mean to jump on you, but we see this all the time. When you ask questions here, please be VERY specific. Describe your exact problem. Show as much of your real code as you have when attempting to solve that problem. Don't generalize. Don't write pseudo-code. Describe your specific problem in enough detail that a consultant that you were paying by the hour could go write you a perfect solution.
No it is enough, but you asked me another question so I answered. Thanks for your help, it guided me in the right direction.
@henry.miller - OK, well that part about condition being met sounds like it's part of your question so that has me confused. If your question is now answered (that would be great), then you can indicate that to the community by clicking the checkmark to the left of the answer.
-2

After a little googling I found this out

js = document.createElement("script");
js.type = "text/javascript"; 
js.src = "thirdparty.js";
document.body.appendChild(js);  //you can append this to your html file head portio

You can refer Correct & simplest way of calling one js file inside another js for more reference

1 Comment

This is how you run a browser-targeted JS file from within a browser. This has nothing to do with node.js.

Your Answer

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