How do I load a regular NodeJS module (from node_modules) from within a TypeScript class?
When I try to compile .ts file that contains:
var sampleModule = require('modulename');
Compiler prompts that I can’t use require in this scope. (That line is at the beginning of the file).
Typescript will always complain when it is unable to find a symbol. The compiler comes together with a set of default definitions for
window,documentand such specified in a file calledlib.d.ts. If I do a grep forrequirein this file I can find no definition of a functionrequire. Hence, we have to tell the compiler ourselves that this function will exist at runtime using thedeclaresyntax:On my system, this compiles just fine.