Pregunta

I'm trying to get RequireJS working using the commonjs var mod=require("mod"); syntax and getting this error:

Uncaught Error: Module name "mod" has not been loaded yet for context: _. Use require([])

The documentation for this error states:

If the error message includes Use require([]), then it was a top-level require call (not a require call inside a define() call) that should be using the async, callback version of require to load the code

But this isn't true for my code - the require line is within a call to define.

Here is my stripped-down test code:

test.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Require JS test</title>
        <script data-main="main" src="require.js"></script>
    </head>
    <body>

    </body>
</html>

main.js:

define(function() {
    var mod=require("mod");
});

mod.js:

define(function() {
    return {
        prop: 123
    };
});

require.js is version 2.1.9 - http://requirejs.org/docs/release/2.1.9/comments/require.js

¿Fue útil?

Solución

Why don't you use it like this:

main js:

require(['mod'], function(mod) {
     console.log(mod.prop);
});

otherwise use:

main.js:

define(function(require) {
    var mod = require("mod");
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top