TypeScript 编译错误 TS5037:除非提供了“--module”标志,否则无法编译外部模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18210630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript compilation error TS5037: Cannot compile external modules unless the '--module' flag is provided
提问by Igor T
Cannot compile any TS+node.js project including listed in samples http://typescript.codeplex.com/sourcecontrol/latest#samples/imageboard/README.txt
无法编译任何 TS+node.js 项目,包括示例http://typescript.codeplex.com/sourcecontrol/latest#samples/imageboard/README.txt
Always get the following error:
总是得到以下错误:
error TS5037: Cannot compile external modules unless the '--module' flag is provided.
错误 TS5037:除非提供了“--module”标志,否则无法编译外部模块。
compiler's version: 0.9.1.0
编译器版本:0.9.1.0
For example, the project consists of just single file app.ts:
例如,该项目仅包含单个文件 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/');
采纳答案by basarat
As mentioned compile with the module flag, e.g. if your file is called myfile.ts
:
如前所述,使用模块标志编译,例如,如果您的文件被调用myfile.ts
:
tsc myfile.ts --module "commonjs"
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.
原因是从 TSC 0.9.1 开始,默认模块选项是 amd(例如 requirejs),这是客户端 javascript 代码最常见的模块模式。因此,您需要指定模块选项来获取 commonjs 代码,这是服务器端 javascript 代码(例如 nodejs)最常见的模块模式,这就是编译器提示您明确目标的原因:) 当您import
在外部模块上做一个。
回答by dburmeister
Also just to add.
也只是补充。
I am using Visual Studio 2013 I got this same error running build to fix it. I went to the properties of my project and then to the "TypeScript Build" section in there was the option to pick a module system I selected AMD it was at none.
我正在使用 Visual Studio 2013 我在运行构建时遇到了同样的错误来修复它。我转到我的项目的属性,然后转到“TypeScript Build”部分,那里有选择模块系统的选项,我选择了 AMD,它没有。