How do I correctly require and declare node library typings in TypeScript?
<p>I am trying to write a package for node in TypeScript that uses standard node libraries, for example <code>fs</code>, <code>path</code>, <code>stream</code>, <code>http</code>, and so on.</p>

<p>When I try to import libraries in a .ts file, VS Code marks the corresponding line with an error:<br>
<code>[ts] Cannot find module 'fs'</code>.
This happens no matter how I try to import the library:</p>

<pre class="lang-typescript prettyprint-override"><code>import * as fs from 'fs';    // [ts] Cannot find module 'fs'
import fs = require('fs');   // [ts] Cannot find module 'fs'
const fs = require('fs');    // [ts] Cannot find name 'require'
</code></pre>

<p><a href="https://i.stack.imgur.com/cO5oQ.png"><img src="https://i.stack.imgur.com/cO5oQ.png" alt="enter image description here"></a></p>

<p>I (should) have the correct definitions installed with <code>typings install --save --ambient node</code>.</p>

<p>When I compile to JavaScript (using <code>gulp</code> and <code>gulp-typescript</code>, not <code>tsc</code>), the compliation <strong>works</strong> and the <strong>syntax highlighting shows no errors</strong> until I type in 1 character again:</p>

<p><a href="https://i.stack.imgur.com/80kJv.png"><img src="https://i.stack.imgur.com/80kJv.png" alt="enter image description here"></a></p>

<p><strong>How can I correctly define the node libraries for TypeScript?</strong></p>

<hr>

<p>I use VS Code for code highlighting and autocompletion, <code>gulp</code> &amp; <code>gulp-typescript</code> to compile and <code>typings</code> for the typescript library declarations.</p>

<p>The project's directory structure:</p>

<pre><code>├─ build/
│  └─ (output files)
├─ src/
│  └─ myfile.ts
├─ typings/
│  ├─ browser/
│  │  └─ ambient/
│  │     └─ node/
│  │        └─ index.d.ts
│  ├─ main/
│  │  └─ ambient/
│  │     └─ node/
│  │        └─ index.d.ts
│  ├─ browser.d.ts
│  └─ main.d.ts
├─ gulpfile.js
├─ package.json
├─ tsconfig.json
└─ typings.json
</code></pre>

<p>My <code>tsconfig.json</code>:</p>

<pre><code>{
    "compileOnSave": false,
    "compilerOptions": {
        "declaration": true,
        "module": "system",
        "moduleResolution": "node",
        "noEmitOnError": true,
        "noImplicitAny": true,
        "target": "es5"
    },
    "exclude": [
        "node_modules",
        "typings/browser",
        "typings/browser.d.ts"
    ]
}
</code></pre>

<p>My <code>typings.json</code>:</p>

<pre><code>{
  "ambientDependencies": {
    "node": "registry:dt/node#4.0.0+20160412142033"
  }
}
</code></pre>

<p>And my gulp task:</p>

<pre><code>gulp.task('build-typescript', () =&gt; {
    const gulpts = require('gulp-typescript');
    const tsProject = gulpts.createProject('tsconfig.json', {
        typescript: require('typescript'),
        outFile: 'mylibrary.js',
        noLib: true
    });

    let tsstream = (
        gulp.src([
            'node_modules/typescript/lib/lib.es6.d.ts',
            'typings/main.d.ts',
            'src/sharpscript.ts'])
        .pipe(sourcemaps.init())
        .pipe(gulpts(tsProject))
    );

    return require('merge2')(
        tsstream.dts.pipe(gulp.dest('build')),
        tsstream.js
            .pipe(sourcemaps.write('.', { includeContent: true }))
            .pipe(gulp.dest('build'))
    );
});
</code></pre>

<p>In case anyone has experienced the same problem, I am thankful for any insights.</p>