title: How to dynamically resolve nodejs required module's path based on caller script's path?
body: <p>I am kind of new to Javascript programming. Currently I am trying to write a test for Javascript files in existing codebase that contains other programming languages. The structure is like below.</p>
  
  <pre><code>src/js/path1/path2/path3/path4/path5/
      Rectangle.jsx
      Circle.jsx
  test/js/path1/path2/path3/path4/path5/
      RectangleTest.jsx
      CircleTest.jsx
  </code></pre>
  
  <p>The content of <code>RectangleTest.jsx</code> is below</p>
  
  <pre><code>import Rectangle from './../../../../../../../src/js/path1/path2/path3/path4/path5/Rectangle';
  
  describe('&lt;Rectangle&gt;', () =&gt; {
      it('Should show content', () =&gt; {
          assert.ok(true);
      });
  });
  </code></pre>
  
  <p>As you can see, I need to set the path as a very long relative path <code>./../../../../../../../src/js/path1/path2/path3/path4/path5</code>. It will be very exhaustive for I prefer something like below.</p>
  
  <pre><code>import Rectangle from './Rectangle';
  </code></pre>
  
  <p>Since the path of the test file and the tested file is pretty similar, it should be possible to calculate the path to be resolved by the import.</p>
  
  <p>Is there a way to do that?</p>
  
  <p>I am using <code>mocha</code> for the testing framework. I uploaded the sample code to Github (<a href="https://github.com/petrabarus/js-test-with-java-file-structure/tree/3976f1030a5fa836d0bf5881209a806e4e07ead8" rel="nofollow noreferrer">link</a>), so you can see it.</p>