title: TypeScript: import external module from node_modules
body: <p>There is a npm module <code>one-two-three</code> for example.
  It contains TS files <code>index.ts</code> (the main) and <code>functions.ts</code>.</p>
  
  <p>functions.ts:</p>
  
  <pre><code>export interface IPoint {
      x: number;
      y: number;
  }
  
  export function sum(a: IPoint, b: IPoint): IPoint {
      return {
          x: a.x + b.x,
          y: a.y + b.y
      };
  }
  </code></pre>
  
  <p>index.ts:</p>
  
  <pre><code>import functions = require("./functions");
  
  export var sum: typeof functions.sum = functions.sum;
  </code></pre>
  
  <p>compile:</p>
  
  <pre><code>tsc --module commonjs --declaration index.ts
  </code></pre>
  
  <p>Files are created: <code>index.js</code>, <code>index.d.ts</code>, <code>functions.js</code> and <code>functions.d.ts</code>.
  Ok.</p>
  
  <p>There is another library that dependent on <code>one-two-three</code>.</p>
  
  <pre><code>npm install --save one-two-three
  </code></pre>
  
  <p>I want to include dependence and use it and the interface from <code>functions.ts</code>.</p>
  
  <pre><code>import mo = require("one-two-three");
  </code></pre>
  
  <p>Error <code>Cannot find external module 'one-two-three'</code>.</p>
  
  <pre><code>/// &lt;reference path="node_modules/one-two-three/index.d.ts" /&gt;
  import mo = require("one-two-three");
  </code></pre>
  
  <p>No reaction.</p>
  
  <pre><code>import mo = require("./node_modules/one-two-three");
  </code></pre>
  
  <p>Fail.</p>
  
  <pre><code>declare var require;
  
  var mo = require("one-two-three");    
  </code></pre>
  
  <p>It compiles successfully.
  But there is no type checking.
  Can write: <code>mo.unknownFunction()</code> and it will be compiled.
  Can not use interfaces.</p>
  
  <p>How to make the above-described in the correct?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have achieved the desired behavior follows.
  Edit d.ts files.</p>
  
  <p>functions.d.ts:</p>
  
  <pre><code>declare module "one-two-three.functions" {
      export interface IPoint {
          x: number;
          y: number;
      }
      export function sum(a: IPoint, b: IPoint): IPoint;
  }
  </code></pre>
  
  <p>index.d.ts:</p>
  
  <pre><code>/// &lt;reference path="./functions.d.ts" /&gt;
  
  declare module "one-two-three" {
      import functions = require("one-two-three.functions");
      export var sum: typeof functions.sum;
  }
  </code></pre>
  
  <p>Using it:</p>
  
  <pre><code>/// &lt;reference path="node_modules/one-two-three/index.d.ts" /&gt;
  /// &lt;reference path="node_modules/one-two-three/functions.d.ts" /&gt;
  
  import oneTwoThree = require("one-two-three");
  import functions = require("one-two-three.functions");
  import IPoint = functions.IPoint;
  
  function delta(a: IPoint, b: IPoint): number {
      var dx: number = a.x - b.x,
          dy: number = a.y - b.y;
      return Math.sqrt(dx * dx + dy * dy);
  }
  
  var point1: IPoint = {x: 10, y: 20},
      point2: IPoint = {x: 5, y: 5};
  
  console.log(oneTwoThree.sum(point1, point2));
  console.log(delta(point1, point2));
  </code></pre>
  
  <p>Success.
  But we have to do double duty. 
  Write code and separately describe the interface.</p>
  
  <p>Is there a way to generate the correct d.ts?
  The problem is that the d.ts should describe the module with internal syntax (<code>module {}</code>).
  But source file is CommonJS module.
  It does not have the section <code>module</code>.</p>