用TypeScript导入节点模块

我试图让这个工作,但我似乎无法find任何地方的解决scheme。 当试图编译这个单一文件的应用程序:

import http = require('http') http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); 

使用命令“tsc app.ts –module'commonjs'”我得到以下错误(不使用–module标志给我一个额外的错误告诉我,我需要它来编译外部模块):

 error TS2071: Unable to resolve external module '"http"'. error TS2072: Module cannot be aliased to a non-module type. 

TypeScript需要知道http是否存在。 遵循这两个步骤

  • 从这里下载node.d.ts文件: https : //github.com/borisyankov/DefinitelyTyped/tree/master/node
  • 在文件顶部添加:

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

PS:查看示例testing文件: https : //github.com/borisyankov/DefinitelyTyped/blob/master/node/node-tests.ts

我发现我有我的tsconfig.json文件中noResolve设置为true 。 这引起了我对TypeScript文件顶部的.d.ts文件的引用错误。

不应该是这样的

 /// <reference path="node.d.ts" /> import http = module('http') 

我的意思是,你不应该使用module而不是require