Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requirejs does not load module synchronously when a context based config is used #662

Closed
khirakawa opened this issue Mar 28, 2014 · 5 comments
Milestone

Comments

@khirakawa
Copy link

StackOverflow link

When using context based configs, requirejs does not load modules synchronously as suggested by the docs:

"... when running in Node, module loading will be loaded using sync IO calls, and loader plugins should resolve calls to their load method synchronously."

Here's my test case:

A.js

define([], function() {
    return {test: "test"};
}

spec.js

var requirejs = require('requirejs');

var localReq = requirejs.config({
    baseUrl: "./",
    context: "context1"
})

describe("context test", function () {
    it("should not throw error", function () {
        for (var i = 0; i < 100; i++) {
            console.log(localReq("A"), i);
        }
    });
});    

When I run the test mocha spec.js, I get the following error:

Uncaught Error: Tried loading "A" at /Users/khirakawa/work/test/node_modules/mocha/bin/A.js then tried node's require("A") and it failed with error: Error: Cannot find module 'A'

screen shot 2014-03-28 at 3 39 32 pm

Interestingly, {test: 'test1'} gets logged 100 times, which seem to suggest that synchronous loading is happening, but there also seems to be something else going on that causes this issue.

Some other observations:

  • If I use an absolute path to A, then it works just fine. i.e. localReq("/Users/khirakawa/work/test/A.js")
  • If I use the default context of "_", it works just fine.
  • I also ran into a similar behavior with jasmine, suggesting that this is more of a requirejs issue.
  • I also modified tests/node/syncRequire/main.js to use a context. The test failed.

Here's my modified main.js test

var requirejs = require('../../../r.js');

var localReq = requirejs.config({
    nodeRequire: require,
    context: "A"
});

var a = localReq('a');

console.log('syncRequire A ' + (a.name === 'a' ? 'PASSED' : 'FAILED'));
console.log('syncRequire A ' + (a.getB().name === 'b' ? 'PASSED' : 'FAILED'));

a is undefined.

Would be great to know why this happens. Thanks!

@jrburke
Copy link
Member

jrburke commented Apr 1, 2014

This error:

Uncaught Error: Tried loading "A" at /Users/khirakawa/work/test/node_modules/mocha/bin/A.js then tried node's require("A") and it failed with error: Error: Cannot find module 'A'

Seems to indicate that it tried to load, but was just at the wrong path. I think the issue is that baseUrl: __dirname in the requirejs config is needed. Otherwise, './' will match the current directory of whatever runs the test, which looks like it might be mocha's bin directory.

Closing as I think that is the issue, but feel free to continue discussion here and we can reopen if there is another issue.

@jrburke jrburke closed this as completed Apr 1, 2014
@lddubeau
Copy link

lddubeau commented Apr 2, 2014

@jrburke , I can reproduce this problem even when using __dirname. One thing though, which I've just discovered now, is that the problem is intermittent. You can do 5 runs without error and the 6th is going to fail. The first time I tried to reproduce this issue (days ago), I was able to reproduce it every single time. Today I get many runs without an issue so there's probably a timing component (maybe the load on my machine changed between the two times I tried). It is also more likely to occur with longer loops.

And here is something that I find puzzling. Even if the interpretation of baseUrl's value were to change during the run, all the calls that load A are of the synchronous form. So they should fail immediately, no? But the error is appearing after all localReq calls have successfully returned a value. So where is the error coming from?

@jrburke
Copy link
Member

jrburke commented Apr 4, 2014

@lddubeau if you have a test case I can try, that will likely be the easiest way to get to the issue.

@lddubeau
Copy link

lddubeau commented Apr 4, 2014

@jrburke Sure! Here's a repo with what I've been running. It is essentially the same as khirakawa's A.js and spec.js tests except that:

  1. My A.js returns "foo" instead of an object.
  2. I use __dirname.
  3. My loop goes to 1000 instead of 100.

@jrburke jrburke added this to the 2.1.12 milestone Apr 30, 2014
@jrburke
Copy link
Member

jrburke commented Apr 30, 2014

Putting this in 2.1.12 bucket for further triage, not sure if it will make 2.1.12 though, or the root cause yet.

@jrburke jrburke reopened this Apr 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants