How to use Node `http` module in TypeScript

I need to write a server in TypeScript and Node.

  • I downloaded Node from the DefinitelyTyped repository
  • I created a TypeScript file
  • Imported Definition
  • Trying to use it

Result:

/// <reference path="definitions/commonjs.d.ts" /> /// <reference path="definitions/node.d.ts" /> var http = require("http"); namespace MyProj { export class Server { public run() { var server = http.createServer(); // TypeScript does not recognize 'http' } } } 

But I can't figure out how I can reference the http module. Where can I find types? In the definition file, it’s hard for me to find out this information.

+5
source share
1 answer

This is due to the use of require . use import , instead it will recognize and also give you a nice intellisense :-)

 import * as http from "http" 
+12
source

All Articles