【问题标题】:Importing an external module in TS causes "ReferenceError: require is not defined"在 TS 中导入外部模块会导致“ReferenceError: require is not defined”
【发布时间】:2012-11-30 01:29:58
【问题描述】:

所以当像这样导入外部模块时

import GameObjects = module("GameObjects")

输出的 JS 在文件顶部有这个:

var GameObjects = require("./GameObjects")

但在 chrome 中运行时会出现此错误:> Uncaught ReferenceError: require is not defined

我如何得到这个错误?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    外部模块需要有一个模块加载器。如果你在浏览器中运行它,你必须自己负责包含一个模块加载器。请查看require.js,它包含运行模块加载器所需的所有文档。

    【讨论】:

    • 我原以为微软会在项目中自动将其连接起来:(
    【解决方案2】:

    如果您想使用内置的 Microsoft 解决方案,请将您的 import 替换为 Triple Slashes

    因此,您可以在文件顶部使用/// <reference path="./Gameobjects.ts" />,而不是import GameObjects = module("GameObjects")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-04
      • 2019-09-29
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2021-03-29
      • 2017-02-25
      相关资源
      最近更新 更多