最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript requirejs issue with loading external modules - Stack Overflow

programmeradmin1浏览0评论

I am having issues with this game.js code that the game.ts file piles to:

var GameObjects = require("./GameObjects")

I have set up my page index.html js imports as so:

<script language="javascript" src="javascripts/require.js" type="text/javascript"> </script>
<script language="javascript" src="javascripts/jquery-1.8.2.min.js" type="text/javascript"> </script>
<script language="javascript" src="ts/GameObjects.js" type="text/javascript"> </script>
<script language="javascript" src="ts/game.js" type="text/javascript"> </script>

and this is the error in chrome:

Uncaught ReferenceError: exports is not defined GameObjects.js:82
Uncaught Error: Module name "GameObjects" has not been loaded yet for context: _. Use require([]) .html#notloaded

any ideas friends?

I am having issues with this game.js code that the game.ts file piles to:

var GameObjects = require("./GameObjects")

I have set up my page index.html js imports as so:

<script language="javascript" src="javascripts/require.js" type="text/javascript"> </script>
<script language="javascript" src="javascripts/jquery-1.8.2.min.js" type="text/javascript"> </script>
<script language="javascript" src="ts/GameObjects.js" type="text/javascript"> </script>
<script language="javascript" src="ts/game.js" type="text/javascript"> </script>

and this is the error in chrome:

Uncaught ReferenceError: exports is not defined GameObjects.js:82
Uncaught Error: Module name "GameObjects" has not been loaded yet for context: _. Use require([]) http://requirejs/docs/errors.html#notloaded

any ideas friends?

Share Improve this question edited Dec 14, 2012 at 16:19 Fenton 251k73 gold badges401 silver badges409 bronze badges asked Dec 13, 2012 at 22:18 NikosNikos 7,55110 gold badges57 silver badges96 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

I'm not sure where the idea of hand-editing the JavaScript files has e from. If you pile your TypeScript with the --module amd flag, you shouldn't need to edit the JavaScript files as the import statements will be converted into require statements.

The only script tag you should need on your page is this:

<script src="javascripts/require.js" data-main="ts/game.js"></script>

Once require.js has loaded, it will then load game.js, and whenever it es across a require statement (which is your import statement in your TypeScript file) it will then load that script and then execute the code.

You can load in jQuery and GameObjects and any other modules just by adding import statements in your TypeScript file.

You have no data-main attribute in your require.js script tag. Please read the RequireJS documentation carefully.

In a nuthshell: you should be loading ts/GameObjects.js from a top-level require call, which you put in the file specified in the data-main attribute. e.g. (from the docs):

<!DOCTYPE html>
<html>
    <head>
        <title>My Sample Project</title>
        <!-- data-main attribute tells require.js to load
             javascripts/main.js after require.js loads. -->
        <script data-main="javascripts/main" src="javascripts/require.js"></script>
    </head>
    <body>
        <h1>My Sample Project</h1>
    </body>
</html>

Then in javascripts/main.js (you can actually call it whatever you want, as long as it matches what you put in data-main), you call require and load your modules and do whatever you want with them:

require(["ts/GameObjects.js", "ts/game.js"], function(GameObjects, Game) {
  ... use 'GameObjects' and 'Game' here ...
});

Remember that in ts/GameObjects.js and ts/game.js you need to wrap your code in define() calls, so they are interpreted as modules.

Hope that helps.

发布评论

评论列表(0)

  1. 暂无评论