typescript 除非提供 --module 标志,否则无法编译模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32170041/
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
Cannot compile modules unless the --module flag is provided
提问by Greg Gum
UPDATE:The issue I had at the time was due to a bug in VS/Typescript. This issue is now resolved. Currently, the only reason for this error to occur is that no module type has been selected in Typescript properties. See accepted answer.
更新:我当时遇到的问题是由于 VS/Typescript 中的一个错误。此问题现已解决。目前,发生此错误的唯一原因是在 Typescript 属性中未选择任何模块类型。请参阅接受的答案。
VS 15: asp.net MVC project.
VS 15:asp.net MVC 项目。
I add a typescript file. I then set the Module system to System.
我添加了一个打字稿文件。然后我将模块系统设置为系统。
I then build the project.
然后我构建项目。
The error occurs: Cannot compile modules unless the --module flag is provided.
出现错误:无法编译模块,除非提供了 --module 标志。
I then check the properties of the ts file. I set the build action to none. The error goes away.
然后我检查 ts 文件的属性。我将构建操作设置为无。错误消失了。
However, if I start with a fresh Typescript project using the template with the Greeter class, then all works as expected.
但是,如果我使用带有 Greeter 类的模板从一个新的 Typescript 项目开始,那么一切都会按预期进行。
But I have looked at the project settings carefully, and there are no differences that I can see.
但是我仔细查看了项目设置,并没有看到我可以看到的差异。
I have read all other stack overflow answers on this error and none of them have resolved the error.
我已经阅读了有关此错误的所有其他堆栈溢出答案,但没有一个解决了该错误。
The only workaround I have found is setting "Compile On Save" to true, and then setting the build action to "None"
我发现的唯一解决方法是将“Compile On Save”设置为 true,然后将构建操作设置为“None”
采纳答案by MarzSocks
1 Open project properties (right click on your project in the Solution Explorer, and click "Properties").
1 打开项目属性(在解决方案资源管理器中右键单击您的项目,然后单击“属性”)。
2 Open the TypeScript tab.
2 打开打字稿选项卡。
3 Choose AMD in the module system section.
3 在模块系统部分选择 AMD。
回答by Allen Rice
So, we have this happen to us all of the time, but for different reasons. We offload all of our typescript compilation to gulp (which is called by a PreBuildEvent), so the compilation works fine, but we still get this error when viewing the file in Visual Studio.
所以,这种情况一直发生在我们身上,但原因各不相同。我们将所有打字稿编译卸载到 gulp(由 PreBuildEvent 调用),因此编译工作正常,但在 Visual Studio 中查看文件时仍然出现此错误。
Basically, we need to tell Visual Studio that we're using AMD, so to accomplish that, do the following:
基本上,我们需要告诉 Visual Studio 我们正在使用 AMD,为此,请执行以下操作:
- In Visual Studio, go to Tools > Options > Text Editor > TypeScript > Project
- Check "Automatically compile TypeScript files which are not part of a project"
- Check "Use AMD code generation for modules that are not part of a project"
- 在 Visual Studio 中,转到“工具”>“选项”>“文本编辑器”>“TypeScript”>“项目”
- 勾选“自动编译不属于项目的 TypeScript 文件”
- 选中“为不属于项目的模块使用 AMD 代码生成”
At that point, you can even uncheck the "Automatically compile ...." option, its just the 2nd one that needs to stay checked.
此时,您甚至可以取消选中“自动编译....”选项,它只是第二个需要保持选中状态的选项。
Again, when weencounter the issue, its a development time issue only and doesn't impact our builds (either locally or on our build servers).
同样,当我们遇到问题时,它只是一个开发时间问题,不会影响我们的构建(本地或构建服务器上)。
回答by Kent Aguilar
Using VS 2013. Here's what solved mine.
使用 VS 2013。这是解决了我的问题。
Go to Project Properties > TypeScript Build > Choose "AMD" as Module system.
转到 Project Properties > TypeScript Build > 选择“AMD”作为模块系统。