简体   繁体   English

打字稿外部模块

[英]Typescript external module

I'm using Visual Studio 2013 Update 3, and have the latest version of WebEssentials installed. 我正在使用Visual Studio 2013 Update 3,并安装了最新版本的WebEssentials。 Under my project settings, under the Typescript Build tab, I've set the Module system type to AMD. 在我的项目设置下,在Typescript Build选项卡下,我将Module系统类型设置为AMD。 I have a Typescript file with the following simple code: 我有一个Typescript文件,其中包含以下简单代码:

export class Test {
}

When I try to build, however, I get the following build error (which also shows up as a red squiggly in my editor): Cannot compile external modules unless the '--module' flag is provided. 但是,当我尝试构建时,我得到以下构建错误(在我的编辑器中也显示为红色波浪形):除非提供了“--module”标志,否则无法编译外部模块。

I've been searching for an answer to this, but it seems like everything online is for pre 1.0 versions of Typescript, and don't seem to apply to my needs. 我一直在寻找这个问题的答案,但似乎在线的所有内容都适用于1.0版本的Typescript,似乎并不适用于我的需求。 Has anyone seen this behavior before, and if so, how did you resolve it? 有没有人见过这种行为,如果有的话,你是怎么解决的?

Thanks in advance for any help! 在此先感谢您的帮助! :) :)

Make sure that the file is included in visual studio as <TypeScriptCompile 确保该文件作为<TypeScriptCompile包含在visual studio中

Also make sure your settings are for both Debug and Release . 还要确保您的设置适用于DebugRelease

The module flag needed refers to what module loader that should be used in order to compile your code. 所需的模块标志指的是为了编译代码应该使用哪个模块加载器。 As you might or might not know, there is AMD and commonjs style modules. 您可能知道或者可能不知道,有AMDcommonjs样式模块。 The file you specified is a module, and in order to refer to it in another TypeScript file, you should define the module loader. 您指定的文件是一个模块,为了在另一个TypeScript文件中引用它,您应该定义模块加载器。 This is because TypeScript compiles to JavaScript. 这是因为TypeScript编译为JavaScript。

This means you have two options for the module flag: 这意味着您有两个模块标志选项:

  • commonjs CommonJS的
  • amd AMD

I suggest you look them up if you don't know anything about them. 如果你对他们一无所知,我建议你查阅一下。 If you go to TypeScript Playground, http://www.typescriptlang.org/Playground#src=export%20class%20Test%20%7B%0D%0A%7D You'll see that this does compile. 如果你去TypeScript Playground, http: //www.typescriptlang.org/Playground#src=export%20class%20Test%20%7B%0D%0A%7D你会看到它确实编译了。 Playground takes amd as module flag to compile. Playground将amd作为模块标志进行编译。

What you want to choose thus depends on whether you are developing for the browser or in node.js. 因此,您要选择的内容取决于您是在为浏览器开发还是在node.js中进行开发。

Hope that helps 希望有所帮助

basarat and froginvasion, thanks to you both for your help. basarat和froginvasion,感谢你们的帮助。 :) It turns out that the problem was with my Visual Studio installation itself. :)事实证明问题出在我的Visual Studio安装本身。 I found that uninstalling Visual Studio 2013 Update 3, and rolling back to Update 2, fixed the issue. 我发现卸载Visual Studio 2013 Update 3并回滚到Update 2修复了该问题。 I'll be submitting a ticket with Microsoft to have this looked at if I can't find anything specific about it online. 如果我在网上找不到任何具体信息,我会向微软提交一张机票。 Regardless, whatever I find, I'll post here. 无论如何,无论我发现什么,我都会在这里发布。 I'll up-vote you both for your effort, and again, thanks so much! 我会为你们的努力投票,再次,非常感谢!

Had the same problem... 有同样的问题......

I found a Typescript Build tab on the project properties. 我在项目属性上找到了一个Typescript Build选项卡。 Module system was set to None. 模块系统设置为无。 Toggling it to AMD fixed the problem for me. 将它切换到AMD为我解决了这个问题。

This tab was off-screeen, and unknown to me -- I think new in Update 2 or 3. The --module message might mention where the setting is. 这个标签是非常筛选的,我不知道 - 我认为更新2或3中有新功能.--模块消息可能会提到设置的位置。

I have been struggling with this exact issue recently and it was right after applying the Visual Studio 2013 update 3. 我最近一直在努力解决这个问题,并且在应用Visual Studio 2013更新3之后就是这样。

Suddenly my AMD modules weren't compiling. 突然间我的AMD模块没有编译。 The module was still set in the project properties and everything. 该模块仍然在项目属性和所有内容中设置。

After beating my head against the wall I decided to comment out the TypeScriptModuleKind nodes in the csproj. 在我的头撞墙后,我决定注释掉csproj中的TypeScriptModuleKind节点。

<TypeScriptModuleKind>amd</TypeScriptModuleKind>

Then, I reloaded the project and opened the typescript build tab. 然后,我重新加载项目并打开typescript构建选项卡。 As expected, the module kind was set to none now. 正如预期的那样,模块类型现在设置为none I re-set it to amd . 我把它重新设置为amd Now when I re-build it succeeds. 现在,当我重新构建它成功。

I opened the .csproj file again to see if it added the TypeScriptModuleKind node and it did, just in a separate property group all by itself. 我再次打开.csproj文件以查看它是否添加了TypeScriptModuleKind节点,它只是在一个单独的属性组中自行添加。

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TypeScriptModuleKind>amd</TypeScriptModuleKind>
</PropertyGroup>

I hope this very strange fix works for everyone else getting the Cannot compile external modules unless the '--module' flag is provided. 我希望这个非常奇怪的修复适用于其他所有人都Cannot compile external modules unless the '--module' flag is provided. error. 错误。

Just to add to this answer, there's a github issue for this that mentions some potential fixes and problems: 只是为了补充这个答案,有一个github问题 ,提到了一些潜在的修复和问题:

  1. For me it was that there was 2 lines that looked like this: 对我来说,有2条线看起来像这样:

And removing the first one seemed to fix it (builds on update 4 as well as update 2). 删除第一个似乎修复它(建立在更新4和更新2)。

  1. Another suggestion is to update a line that looks like 另一个建议是更新看起来像的行

to

<Import Project="$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets" Condition="Exists('$(VSToolsPath)\TypeScript\Microsoft.TypeScript.targets')" />

But that didn't seem to work for me. 但这似乎对我不起作用。

  1. Another suggestion was to ensure that the amd declaration was before the typescript import line, like this: 另一个建议是确保amd声明在typescript导入行之前,如下所示:

<PropertyGroup> <TypeScriptModuleKind>amd</TypeScriptModuleKind> </PropertyGroup> <Import Project="$(VSToolsPath)\\TypeScript\\Microsoft.TypeScript.targets" condition="Exists('$(VSToolsPath)\\TypeScript\\Microsoft.TypeScript.targets')" />

Which I didn't try. 我没试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM