title: How to avoid circular dependency between modules when working with recursive functions?
body: <p>The following code traverses an object and its keys. It does a thing for each key depending on what kind of value it refers to. It works well. All is good.</p>
  <pre><code>const obj = {
    prop1: {
      prop2: 1,
      prop3: {
        prop4: 2
      }
    },
    prop5: &quot;1&quot;
  }
  
  function inspectRecursively(node) {
    switch (typeof node) {
      case &quot;object&quot;:
        handleObject(node);
        break;
      case &quot;string&quot;:
        handleString(node);
        break;
      case &quot;number&quot;:
        handleNumber(node);
        break;
      default:
        break;
    }
  }
  
  function handleObject(node) {
    console.log(JSON.stringify(node, null, 2))
      
    for (const [key] of Object.entries(node)) {
      inspectRecursively(node[key]);
    }
  }
  
  function handleString(node) {
    console.log(node)
  }
  
  function handleNumber(node) {
    console.log(node.toString().padStart(&quot;0&quot;, 3))
  }
  
  inspectRecursively(obj)
  </code></pre>
  <p>Say that I think that the file has grown too large. I split it up into modules</p>
  <p><strong>main.js</strong></p>
  <pre><code>import { importRecursively } from &quot;./importRecursively&quot;
  
  const obj = {
    prop1: {
      prop2: 1,
      prop3: {
        prop4: 2
      }
    },
    prop5: &quot;1&quot;
  }
   inspectRecursively(obj)
  </code></pre>
  <p><strong>importRecursively.js</strong></p>
  <pre><code>import { handleObject } from &quot;./handleObject.js&quot;
  import { handleString} from &quot;./handleString.js&quot;
  import { handleNumber} from &quot;./handleNumber.js&quot;
  
  function inspectRecursively(node) {
    switch (typeof node) {
      case &quot;object&quot;:
        handleObject(node);
        break;
      case &quot;string&quot;:
        handleString(node);
        break;
      case &quot;number&quot;:
        handleNumber(node);
        break;
      default:
        break;
    }
  }
  </code></pre>
  <p><strong>handleObject.js</strong></p>
  <pre><code>import { importRecursively } from &quot;./importRecursively&quot;
  
  function handleObject(node) {
    console.log(JSON.stringify(node, null, 2))
      
    for (const [key] of Object.entries(node)) {
      inspectRecursively(node[key]);
    }
  }
  </code></pre>
  <p><strong>handleString.js</strong></p>
  <pre><code>function handleString(node) {
    console.log(node)
  }
  </code></pre>
  <p><strong>handleNumber.js</strong></p>
  <pre><code>function handleNumber(node) {
    console.log(node.toString().padStart(&quot;0&quot;, 3))
  }
  </code></pre>
  <p>Now I end up with a circular dependency.</p>
  <pre><code>main -&gt; inspectRecursively -&gt; handleObject -&gt; importRecursively
  </code></pre>
  <p>I think this is bad, but am not sure about it?</p>
  <p>What do I do in this situation? Do I change something to avoid the circular dependency?</p>