【问题标题】:TypeScript compilation error TS5037: Cannot compile external modules unless the '--module' flag is providedTypeScript 编译错误 TS5037:除非提供“--module”标志,否则无法编译外部模块
【发布时间】:2013-08-13 13:36:07
【问题描述】:

无法编译任何 TS+node.js 项目,包括示例 http://typescript.codeplex.com/sourcecontrol/latest#samples/imageboard/README.txt 中列出的项目

总是得到以下错误:

错误 TS5037:除非提供“--module”标志,否则无法编译外部模块。

编译器版本:0.9.1.0

例如,项目只包含单个文件 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/');

【问题讨论】:

    标签: compiler-construction typescript


    【解决方案1】:

    也只是添加。

    我正在使用 Visual Studio 2013 运行构建来修复它时遇到了同样的错误。我转到我的项目的属性,然后转到“TypeScript Build”部分,那里有一个选择模块系统的选项,我选择了 AMD,它没有。

    【讨论】:

    • 我也有“无”,这很完美,谢谢。我想补充的另一件事是,如果您没有让 Typescript 知道您的外部模块,您仍然可能会出现不同的错误。像这样的东西:declare module 'angular' { var angular: ng.IAngularStatic; export = angular; }
    • 我已经这样做了,但我仍然得到错误。现在开始怀疑这是否是 VS2015 的错误。
    【解决方案2】:

    如前所述,使用模块标志编译,例如如果您的文件名为myfile.ts

    tsc myfile.ts --module "commonjs" 
    

    原因是从 TSC 0.9.1 开始,默认模块选项是 amd(例如 requirejs),这是客户端 javascript 代码最常见的模块模式。因此,您需要指定模块选项来获取 commonjs 代码,这是服务器端 javascript 代码(例如 nodejs)最常见的模块模式,这就是编译器提示您明确目标的原因:) 当您出现此提示时在外部模块上执行import

    【讨论】:

    • 非常感谢,成功了!
    【解决方案3】:

    在 GraphQL 示例中遇到类似问题,我曾在其中使用 create-react-app myapp --typescript。

    TS1208

    能够像这样修复它:

    【讨论】:

      猜你喜欢
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 2019-12-30
      相关资源
      最近更新 更多