RequireJS calling callbacks before dependencies lo

2019-06-26 03:23发布

问题:

I'm having an issue with RequireJS where my main.js script has a reference to a dependency, which is loaded but not resolved when the callback in main.js requesting this dependency is run.

My directory structure is:

index.htm
scripts/
    require.js
    main.js
    feeds/
        feed.js

index.htm:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Blah</title>
        <script data-main="scripts/main" src="scripts/require.js"></script>
    </head>
    <body>
        <h1>Hello, World!</h1>
    </body>
</html>

main.js:

require(["feeds/feed"], function(feed) {
    console.log("A");
    require.ready(function() {
        console.log("B");
        console.log(feed.val);
    });
});

feed.js:

console.log("C");
require(function() {
    console.log("D");
    return {
        val: "E"
    }
})

And the console output, suggesting that the dependency files are being loaded, but not resolved correctly:

C
A
B
Uncaught TypeError: Cannot read property 'val' of null

I must be missing something really obvious here, but whatever documentation I read up on, the problem doesn't seem to be revealing itself. Any ideas?

回答1:

To answer my own stupid question:

You're using require to define your modules, where you should be using define.

Facepalm.