134

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

3
  • 1
    Can you show us your code ( original and compiled one )? I thought that require can be used anywhere, it does not depend on scope.
    – freakish
    Oct 5, 2012 at 8:10
  • Yes, but have to be declared and is not declared in default lib.d.ts as Valentin suggested. Oct 5, 2012 at 8:42
  • TypeScript should not complain if the target is set to ES6 using "target": "es6", in the compilerOptions in tsconfig.json.
    – Mike Poole
    Oct 14, 2022 at 6:22

5 Answers 5

130

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.

8
  • 25
    Thank you. This will work for all functions not declared. Better solution I have found is to get node.d.ts from typescript samples and reference it. Than you have everything. There is also express.d.ts. Oct 5, 2012 at 8:41
  • 3
    I think you can also do import sampleModule = module('modulename')
    – mpen
    Jan 31, 2013 at 20:23
  • Yeah but node.d.ts is currently at v0.8.8. Better to use the approach by @Valentin and then use say WebStorm IDE that can download definitions for the version of Node you are running and give you autocomplete etc on that.
    – Radek
    Jun 21, 2013 at 18:56
  • @Radek How do you do that? I use WebStorm 7.0.2 (latest) Btw I don't find the script node.d.ts, where does it is located? Thx. Nov 26, 2013 at 19:19
  • 1
    @Vadorequest this should help: jetbrains.com/webstorm/webhelp/…
    – Radek
    Nov 27, 2013 at 13:19
94

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

1
  • I followed this approach, using "import * as $s from 'scriptjs';" since I am using TypeScript. The CKEDITOR.replace() command seems to execute, and I get what looks like a valid DOM element from it, however nothing changes in the browser (still a textarea) and my editor.on( 'contentDom', ...) callbacks (also installed inside the $s callback) never get called. Any idea what's up? Apr 19, 2017 at 2:54
57

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.

3
  • 1
    This worked for me too! But what I don't understand is that I had it installed globally (ie: npm install -g @types/node) but that didn't work?
    – Jeach
    Mar 17, 2019 at 15:30
  • 1
    What if types module is not available in npm ? Jan 24, 2020 at 12:18
  • @SudharshanNair there are a lot of types defined by the DefinitelyTyped project. Other options include contrinuting to Definitely typed declaring types within your own project, or using without type defs,
    – rharriso
    Mar 16, 2020 at 5:14
3

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
1
  • 27
    Should be noted that typings is deprecated in favor of simply npm install @types/node --save-dev Apr 9, 2017 at 13:41
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.