Search code examples
node.jstypescriptwebstorm

What's a correct way to work with Node.js and TypeScript?


I just started to use Node.JS with TypeScript and the very first thing I stumbled was a compilation error (using WebStorm) for the very first line:

import http = require('http');

I tried to use "var", "module" instead of "require" but I was still getting "cannot find module http" or "cannot understand 'require' name". After a while I was able to find only one solution (@basarat) at stackoverflow, which suggested to download and add to project some node.d.ts file, and then use it as a reference at the top:

/// <reference path="node.d.ts" />

import http = require('http');

However, it doesn't look very nice as I expecting the product from Microsoft would work more or less out of box without the need to use 3rd party hacks. Also, it looks like I will need to add this reference file to every file I will need to use node.js modules. How do you deal with that?


Solution

  • However, it doesn't look very nice as I expecting the product from Microsoft would work more or less out of box without the need to use 3rd party hacks

    The original file came from Microsoft. It's just community maintained. Also using open source is not "third party hacks". Using definition files is supported by the Typescript compiler.

    Also, it looks like I will need to add this reference file to every file I will need to use node.js modules.

    You can aggregate all these definition file referenced into a 'vendor.d.ts' and then this is the only file you need to reference. Also this reference can be codegened : https://github.com/TypeStrong/grunt-ts/blob/master/README.md#transforms