How to NodeJS require inside TypeScript file?

node.jsTypescript

node.js Problem Overview


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).

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

The correct syntax is:

import sampleModule = require('modulename');

or

import * as sampleModule from 'modulename';

Then compile your TypeScript with --module commonjs.

If the package doesn't come with an index.d.ts file and its package.json doesn't have a "typings" property, tsc will bark that it doesn't know what 'modulename' refers to. For this purpose you need to find a .d.ts file for it on http://definitelytyped.org/, or write one yourself.

If you are writing code for Node.js you will also want the node.d.ts file from http://definitelytyped.org/.

Solution 3 - node.js

The best solution is to get a copy of Node's type definitions. This will solve all kinds of dependency issues, not only require(). This was previously done using packages like typings, but as Mike Chamberlain mentioned, Typings are deprecated. The modern way is doing it like this:

npm install --save-dev @types/node

Not only will it fix the compiler error, it will also add the definitions of the Node API to your IDE.

Solution 4 - node.js

Use typings to access node functions from TypeScript:

typings install env~node --global

If you don't have typings  install it:

npm install typings --global

Solution 5 - node.js

when loading typescript module I use module name with extension i.e. ".ts"

const sampleModule = require('modulename.ts');

FYI, in my case "node" command (v14.17.1) using ".ts" files directly without generating "*.js" files.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZdenek SejcekView Question on Stackoverflow
Solution 1 - node.jsValentinView Answer on Stackoverflow
Solution 2 - node.jsJesseView Answer on Stackoverflow
Solution 3 - node.jsrharrisoView Answer on Stackoverflow
Solution 4 - node.jsOded BreinerView Answer on Stackoverflow
Solution 5 - node.jsGLKView Answer on Stackoverflow