Thursday, 1 June 2017

Is requiring a file dynamically in nodejs creates cyclic dependency.?

I have a index.js file in directory, which requires all files of a folder dynamically as below

'use strict';
var fs = require('fs');

var map = {};

function initialize() {
  fs.readdir('mydir', function (err, files) {
    for (var i in files) {
      if (files[i].indexOf(".js") !== -1) {
        map[i] = require('./' + files[i]);
      }
    }
  });
}

initialize();

module.exports = map;

Each files in directory also requires the index files as below

var dir = require('../mydir');

So will this create cyclic dependency??

If yes, how to resolve the same, my basic purpose is that each file in a directory can call other files function.



via Anuj Nautiyal

No comments:

Post a Comment