Skip to content

Instantly share code, notes, and snippets.

@EloB
Created January 16, 2015 15:25
Show Gist options
  • Save EloB/013294dfc754a0d3baa5 to your computer and use it in GitHub Desktop.
Save EloB/013294dfc754a0d3baa5 to your computer and use it in GitHub Desktop.
Node require recursion example
exports.hello = 'world1';
exports.file2 = require('./file2');
exports.file3 = require('./file3');
exports.hello = 'world2';
exports.file3 = require('./file3');
module.exports = {
hello: 'world3',
file1: require('./file1'),
file2: require('./file2'),
file4: require('./file4')
};
module.exports = {
file3: require('./file3')
};
console.log('file1', require('./file1'));
console.log('file2', require('./file2'));
console.log('file3', require('./file3'));
console.log('file4', require('./file4'));
@EloB
Copy link
Author

EloB commented Jan 16, 2015

Output:

file1 { hello: 'world1',
  file2: 
   { hello: 'world2',
     file3: 
      { hello: 'world3',
        file1: [Circular],
        file2: [Circular],
        file4: [Object] } },
  file3: 
   { hello: 'world3',
     file1: [Circular],
     file2: { hello: 'world2', file3: [Circular] },
     file4: { file3: {} } } }
file2 { hello: 'world2',
  file3: 
   { hello: 'world3',
     file1: { hello: 'world1', file2: [Circular], file3: [Circular] },
     file2: [Circular],
     file4: { file3: {} } } }
file3 { hello: 'world3',
  file1: 
   { hello: 'world1',
     file2: { hello: 'world2', file3: [Circular] },
     file3: [Circular] },
  file2: { hello: 'world2', file3: [Circular] },
  file4: { file3: {} } }
file4 { file3: {} }

Pay attention to the last 2 rows. It gets tricky there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment