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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:50:58  来源:igfitidea点击:

TypeScript compilation error TS5037: Cannot compile external modules unless the '--module' flag is provided

compiler-constructiontypescript

提问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 importon 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,它没有。

回答by John Peters

Was having similar issue with the GraphQL samples, where I had used create-react-app myapp --typescript.

GraphQL 示例也有类似的问题,我在其中使用了 create-react-app myapp --typescript。

TS1208

TS1208

GraphQL compiler error

GraphQL 编译器错误

Was able to fix it like this:

能够像这样修复它:

enter image description here

在此处输入图片说明