0

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

1 Answer 1

1

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");
});
Sign up to request clarification or add additional context in comments.

4 Comments

I am interested in why the way I'm doing it now doesn't work.
i think you were just missing the require arg in your define function
var mod = require("mod"); won't work as require is async use define(["mod"], function(mod, require) {
@megawac : you shouldn't require mod first, you should use define(["require","mod"], function(require,mod) {}); instead, but the example above works too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.