<p dir="auto">Original report : <a href="http://stackoverflow.com/questions/28844917/typescript-cant-find-external-module-when-referenced-by-two-different-files" rel="nofollow">http://stackoverflow.com/questions/28844917/typescript-cant-find-external-module-when-referenced-by-two-different-files</a></p>
<p dir="auto">Given the structure :</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="./first/shared.ts
./first/second/class_a.ts
./first/second/class_b.ts
./third/class_c.ts"><pre class="notranslate"><code class="notranslate">./first/shared.ts
./first/second/class_a.ts
./first/second/class_b.ts
./third/class_c.ts
</code></pre></div>
<p dir="auto">and files :</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="shared.ts:
class Shared{}
export = Shared;

class_a.ts:
import Shared = require('../shared');
class A{}
export = A;

class_b.ts:
import A = require('./class_a');
import C = require('../../third/class_c');
class B {}
export = B;

class_c.ts:
import Shared = require('../first/shared');
class C {}
export = C;"><pre class="notranslate"><code class="notranslate">shared.ts:
class Shared{}
export = Shared;

class_a.ts:
import Shared = require('../shared');
class A{}
export = A;

class_b.ts:
import A = require('./class_a');
import C = require('../../third/class_c');
class B {}
export = B;

class_c.ts:
import Shared = require('../first/shared');
class C {}
export = C;
</code></pre></div>
<p dir="auto">Compiling class_b.ts fails with the error that it can't find shared.ts:<br>
<code class="notranslate">tsc --module commonjs class_b.ts</code></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="../../third/class_c.ts(1,25): error TS2307: Cannot find external module '../first/shared'."><pre class="notranslate"><code class="notranslate">../../third/class_c.ts(1,25): error TS2307: Cannot find external module '../first/shared'.
</code></pre></div>
<p dir="auto">It should not error.</p>
<p dir="auto"><strong>Note that it doesn't happen in the language service</strong><br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/874898/6476486/757c580c-c26a-11e4-9342-e04b102eb21f.png"><img src="https://cloud.githubusercontent.com/assets/874898/6476486/757c580c-c26a-11e4-9342-e04b102eb21f.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">(Also posted on StackOverflow: <a href="https://stackoverflow.com/questions/28844917/typescript-cant-find-external-module-when-referenced-by-two-different-files" rel="nofollow">https://stackoverflow.com/questions/28844917/typescript-cant-find-external-module-when-referenced-by-two-different-files</a>)</p>
<p dir="auto">I may be missing something obvious, but Typescript's module resolver for commonjs isn't working as expected.</p>
<p dir="auto">Given the following directory structure and files:</p>
<ul dir="auto">
<li>./first/shared.ts</li>
<li>./first/second/class_a.ts</li>
<li>./first/second/class_b.ts</li>
<li>./third/class_c.ts</li>
</ul>
<p dir="auto">Where:</p>
<ul dir="auto">
<li>class_a references shared.ts</li>
<li>class_b references class_a.ts and class_c.ts</li>
<li>class_c references shared.ts</li>
</ul>
<p dir="auto">Specifically:</p>
<p dir="auto">shared.ts:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class Shared{}
export = Shared;"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-smi">Shared</span><span class="pl-kos">{</span><span class="pl-kos">}</span>
<span class="pl-k">export</span> <span class="pl-c1">=</span> <span class="pl-smi">Shared</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">class_a.ts:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import Shared = require('../shared');
class A{}
export = A;"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-smi">Shared</span> <span class="pl-c1">=</span> require<span class="pl-kos">(</span><span class="pl-s">'../shared'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">class</span> <span class="pl-smi">A</span><span class="pl-kos">{</span><span class="pl-kos">}</span>
<span class="pl-k">export</span> <span class="pl-c1">=</span> <span class="pl-smi">A</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">class_b.ts:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import A = require('./class_a');
import C = require('../../third/class_c');
class B {}
export = B;"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-smi">A</span> <span class="pl-c1">=</span> require<span class="pl-kos">(</span><span class="pl-s">'./class_a'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-smi">C</span> <span class="pl-c1">=</span> require<span class="pl-kos">(</span><span class="pl-s">'../../third/class_c'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">class</span> <span class="pl-smi">B</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>
<span class="pl-k">export</span> <span class="pl-c1">=</span> <span class="pl-smi">B</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">class_c.ts:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import Shared = require('../first/shared');
class C {}
export = C;"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-smi">Shared</span> <span class="pl-c1">=</span> require<span class="pl-kos">(</span><span class="pl-s">'../first/shared'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">class</span> <span class="pl-smi">C</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>
<span class="pl-k">export</span> <span class="pl-c1">=</span> <span class="pl-smi">C</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">All compile individually, except class_b.ts, using the following compiler invocation:</p>
<p dir="auto"><code class="notranslate">tsc --module commonjs class_b.ts</code></p>
<p dir="auto">Compiling class_b.ts fails with the error that it can't find shared.ts:</p>
<p dir="auto"><code class="notranslate">../../third/class_c.ts(1,25): error TS2307: Cannot find external module '../first/shared'.</code></p>
<p dir="auto">Note that if you reverse the order of imports in class_b.ts, you get a different error:</p>
<p dir="auto"><code class="notranslate">class_a.ts(1,25): error TS2307: Cannot find external module '../shared'.</code></p>
<p dir="auto">It seems the compiler is finding shared.ts the first time it is imported, but not the second time.</p>
<p dir="auto">This is with tsc 1.4.1.0.</p>