在 TS 中导入外部模块导致“ReferenceError: require is not defined"; [英] Importing an external module in TS causes "ReferenceError: require is not defined"

查看:467
本文介绍了在 TS 中导入外部模块导致“ReferenceError: require is not defined";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So when importing an external module like so

import GameObjects = module("GameObjects")

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

the outputed JS has this at the top of the file:

var GameObjects = require("./GameObjects")

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

but gives this error when run in chrome:> Uncaught ReferenceError: require is not defined

我该如何解决这个错误?

how do I get by this error?

推荐答案

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

External modules require a module loader to be present. If you run this in your browser you have to take care of including a module loader yourself. Please take a look at require.js, it has all the documentation that's needed to get the module loader running.

这篇关于在 TS 中导入外部模块导致“ReferenceError: require is not defined";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆