0

I have two .js files: /Desktop/firstFolder/first.js and /Desktop/secondFolder/second.js. I want first to work first, then it executes second. In the end of second I want it to execute first again and so on. Sounds simple, right?

first works fine, then second is doing it's job and then it stops working. Why can't I execute first for the second time?

So first and second scripts are as simple as possible:

console.log('first.js works, launching second.js');
process.chdir('/Users/apple/Desktop/secondFolder');
require('/Users/apple/Desktop/secondFolder/second.js');
console.log('second.js works, launching first.js');
process.chdir('/Users/apple/Desktop/firstFolder');
require('/Users/apple/Desktop/firstFolder/first.js');

Logs from terminal:

first.js works, launching second.js
second.js works, launching first.js
Apples-MacBook-Air:firstFolder apple$ 

Why does it stop working? Does javascript prevent itself from infinite loop? Is there a way to make it happen?

There was a suggested question but it's different because they ask how to make one .js file execute another .js file multiple times instead of two .js files executing each other. What is more, I tried to do the same with my code, I wrap both .js files in module.exports = function() {} and added an extra () at the end of each require but now it throws an error:

  require('/Users/apple/Desktop/firstFolder/first')();
                                                   ^

TypeError: require(...) is not a function

narra_kk
  • 111
  • 6
  • Does this answer your question? [Require multiple times](https://stackoverflow.com/questions/52359789/require-multiple-times) – VLAZ Jan 17 '20 at 17:50
  • It throws an error. `TypeError: require(...) is not a function` – narra_kk Jan 17 '20 at 18:04

3 Answers3

3

The Node module system is built for modules, which means the semantics of require are to “require”, not “execute”. Two modules can’t logically require each other mutually simply in order to exist, but their exports can, so that’s the behaviour Node gives you with recursive require: you get the current value of the exports object of a module that’s already running.

So export your functionality from modules instead:

const second = require('/Users/apple/Desktop/secondFolder/second.js');

exports.run = () => {
    console.log('first.js works, launching second.js');
    process.chdir('/Users/apple/Desktop/secondFolder');
    second.run();
};
const first = require('/Users/apple/Desktop/firstFolder/first.js');

exports.run = () => {
    console.log('second.js works, launching first.js');
    process.chdir('/Users/apple/Desktop/firstFolder');
    first.run();
};

This should throw a stack overflow error. (If you don’t want a stack overflow error, replace the run calls with something that doesn’t preserve the stack, like process.nextTick(first.run).)

Ry-
  • 218,210
  • 55
  • 464
  • 476
  • It doesn't throw a stack overflow error. Nothing happens instead. No logs in terminal, no nothing. – narra_kk Jan 17 '20 at 18:14
  • @narra_kk: You have to start it off by calling the exported function. From a third file, or `if (require.main === exports) exports.run()`. – Ry- Jan 17 '20 at 18:21
  • I tried adding `if (require.main === exports) exports.run()` in various places, but still nothing happens. How do I call it from a third file? – narra_kk Jan 17 '20 at 18:25
3

It looks like 'executing the file' just means requiring it, because you don't call any function after requiring it in your code.

Node doesn't execute a required file twice. Rather, the first time you require a file, it executes the whole file and generates something from your module.exports, and then if you require the same file again, it simply gives you your module.exports again.

So, what you should do is have your files both export the function that you want to execute, and execute that function in each file.

Eg in file 2 you have module.exports = function(){...}. And in file 1, you do var file2 = require('./file2'); file2();

And then do the same thing for executing file1 inside file2.

And you might then want an entry file that requires file1 and then calls it, var file1 = require('./file1'); file1();

TKoL
  • 13,158
  • 3
  • 39
  • 73
0

JS files are stored in cache once called via require() in nodejs, thus console is called only once and when both files are loaded it waits for events to happen or other functions to execute.

You can read more in https://nodejs.org/api/modules.html#modules_require_id

If you want forever loop use one of for, foreach, while.

Vishvendra Singh
  • 484
  • 5
  • 19