Import Node Modules into TypeScript

How to import Node modules that are in the node_modules folder in TypeScript?

I get an error ( The name "async" does not exist in the current scope ) when I try to compile the following TypeScript code snippet:

// Converted from: var async = require('async'); import async = module('async'); 
+4
source share
3 answers

You need to create a definition file and include it:

 module "async" {} 

then add a link to this definition file in your TypeScript code

 ///<reference path='definition-file.d.ts' /> import async = module('async'); 
+1
source

The standard "import blah = require ('x')" semantics in typescript does not work with node modules.

The easiest way to get around this is to define and use the interface and use it explicitly, rather than import:

 // You can put this in an external blah.d file if you like; // remember, interfaces do not emit any code when compiled. interface qOrm { blah:any; } declare var require:any; var qOrm:qOrm = require("../node_modules/q-orm/lib/q-orm"); var x = qOrm.blah; 
+1
source
 npm install --save-dev @types/async npm install --save async 

And the syntax is:

 import * as async from "async" 
0
source

Source: https://habr.com/ru/post/1446023/


All Articles