I am wondering how to load regular nodejs module from node_modules
directory from inside TypeScript
class.
When I try to compile .ts
file that contains:
var sampleModule = require('modulename');
compiler tells me I cannot use require in this scope. (that line is at the beginning of the file).
Is there a way how to load and use modules from node_modules
inside TypeScript
class?
Typescript will always complain when it is unable to find a symbol. The compiler comes together with a set of default definitions for window
, document
and such specified in a file called lib.d.ts
. If I do a grep for require
in this file I can find no definition of a function require
. Hence, we have to tell the compiler ourselves that this function will exist at runtime using the declare
syntax:
declare function require(name:string);
var sampleModule = require('modulename');
On my system, this compiles just fine.