36

我试图让它工作,但我似乎无法在 SO 的任何地方找到解决方案。尝试编译此单文件应用程序时:

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

3 回答 3

45

TypeScript 需要知道它http的存在。

更新

为节点安装类型定义:

npm install @types/node

旧答案

遵循这两个步骤

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

于 2013-08-22T12:15:49.003 回答
2

我发现我已经在我的 tsconfig.json 文件中noResolve设置了。true这导致对我在 TypeScript 文件顶部包含的 .d.ts 文件的引用出现错误。

于 2016-07-17T11:19:47.487 回答
-3

不应该是这样的

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

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

于 2014-01-15T10:55:49.437 回答