I currently have a program structured like the minimal example below. I have object foo
which
has method addColor
, which requires module bar
.
bar
requires baz
and passes the parameter color
.
baz
requires foo
to edit a property from foo
(addColor
must add a color to foo.colors
)
However inside baz
: foo
returns {}
and the following error occures.
foo.colors.push(color);
^
TypeError: Cannot read property 'push' of undefined
I imagine this is an issue with module recursion and cannot find a work-around to this problem without possibly restructuring my program.
index.js
const foo = require('./foo');
console.log(foo);
// { colors: [ 'red', 'green' ], addColor: [Function (anonymous)] }
foo.addColor('blue');
foo.js
module.exports = {
colors: ['red', 'green'],
addColor: require('./bar')
}
bar.js
const baz = require('./baz');
module.exports = color => {
baz(color);
};
baz.js
const foo = require('./foo');
module.exports = color => {
console.log(foo); // {}
foo.colors.push(color);
/*
foo.colors.push(color);
^
TypeError: Cannot read property 'push' of undefined
*/
};