You can also see : Using and Running Node.js Modules in Browser
The short answer is definitely YES ! but you need some intermediate steps!
There are many scenarios that we need to cover i.e in the browser and in a node environment. So let’s started.
Either create typings for that library but this process can be time consuming and needs a deep knowledge of the library internals.
Or you can just follow these simple steps to include the library without using typings.
First include the library source file before the compiled TypeScript file of your project, using script tag in your HTML file.
Next, In your TypeScript file before using the library, add
declare var libGlobal: any;
You need to replace libGlobal with your library global object which gives access to the library API.
Then just use any library function just like normal.
Using external libraries with TypeScript project for Node environment
Let’s suppose you have installed some node library using npm :
npm install --save mylib
If you try to use the import system of TypeScript to import this lib
import * from "mylib";
This will fail with errors such as :
........: error TS2307: Cannot find module 'mylib'.
This can be solved by installing typing for mylib if they do exist.
In case typings for your li don’t exit you’ll try to use the usually required style of Node
var mylib = require("mylib");
This also will fail with error :
.......: error TS2304: Cannot find name 'require'.
This means TypeScript can not recognize require statement. But this can be easily solved by installing the typings for node, which are available from npm, using :
npm install @types/node --save