Best practice to include an npm module in Typescript


I found this way to include an npm module (in my example is async) with typescript:

declare var require: {
    <T>(path: string): T;
    (paths: string[], callback: (...modules: any[]) => void): void;
    ensure: (paths: string[], callback: (require: <T>(path: string) => T) => void) => void;

var async = require('async');

Is this the best way to do it?

How to add an npm of AngularJS 1.x into Ionic 2 project?

What you can do is use a typings definitions file to tell typescript about the code.

typings install aync --ambient --save

Now you should be able to import async like so.

import * as async from 'async';

And all should work.


@mhartington: is there a reason you recommend going straight to the --ambient flag? I consider it a last resort, because if you can install without it, you know you are using a tsd that has been vetted by the typings registry and get some extra versioning and namespace pollution protection.


Typings registry isn’t as populous as Definitely Typed. For this case, async is not in typings, but is in definitely typed


I was referring more to your blog post giving generalized advice, which would have people install from DT even for libraries that are in the typings registry (which is growing fairly quickly).