TypeScript编译错误TS5037:除非提供了“--module”标志,否则无法编译外部模块 [英] TypeScript compilation error TS5037: Cannot compile external modules unless the '--module' flag is provided
问题描述
无法编译任何TS + node.js项目,包括示例 http:// typescript中列出的项目。 codeplex.com/sourcecontrol/latest#samples/imageboard/README.txt
Cannot compile any TS+node.js project including listed in samples http://typescript.codeplex.com/sourcecontrol/latest#samples/imageboard/README.txt
始终会遇到以下错误:
错误TS5037:除非提供了--module标志,否则无法编译外部模块。
error TS5037: Cannot compile external modules unless the '--module' flag is provided.
编译器版本:0.9.1.0
compiler's version: 0.9.1.0
例如,该项目只包含单个文件app.ts:
For example, the project consists of just single file app.ts:
///<reference path="./node_definitions/node.d.ts" /
import http = require("http")
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, 'localhost');
console.log('Server running at http://localhost:1337/');
推荐答案
如上所述用模块标志编译,例如如果您的文件名为 myfile.ts
:
As mentioned compile with the module flag, e.g. if your file is called myfile.ts
:
tsc myfile.ts --module "commonjs"
原因是从TSC 0.9.1开始,默认模块选项是amd(例如requirejs),这是客户端JavaScript代码的最常见的模块模式。所以,你需要指定模块选项来获得commonjs代码,这是服务器端JavaScript代码(例如nodejs)最常见的模块模式,这就是为什么编译器提示你显式关于你的目标:)这个提示发生时,你在外部模块上执行 import
。
The reason is that starting from TSC 0.9.1 the default module option is amd (e.g. requirejs) which is the most common module pattern for client side javascript code. So, you need to specify the module option to get commonjs code which is the most common module pattern for server side javascript code (e.g. nodejs) which is why the compiler is prompting you to be explicit about your target :) This prompt occurs when you do an import
on an external module.
这篇关于TypeScript编译错误TS5037:除非提供了“--module”标志,否则无法编译外部模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!