How to change object(in same lib but different path) at same time in NodeJS

My project folder like this:

  • --main.js
  • --lib
  • ------libA
  • ----------a.js
  • ------libB
  • ----------b.js
  • ----------lib
  • --------------libA
  • ------------------a.js

In main.js:

var obja = require('./lib/libA/a');
require('./lib/libB/b');

In b.js:

var obja = require('./lib/libA/a');

In a.js:

module.exports = {};

Then if I changed obja in b.js, obja in main.js did not change.

My question is how to change two obja at the same time.

Thanks.

In your example, if you get rid of the obja variable and just reference libA.a you can change it in both places that way - assuming you are in the same process / cluster

You have two different a.js files — one in lib/libA, and the other in lib/libB/lib/libA. Those are two different modules, and each of them has its own exports object. If you want to reference the same a module, you need to change your b.js file to:

var obja = require('../libA/a');

This way, both main.js and b.js will refer to the same module (the one in lib/libA/a.js).