1

I've got a directory structure like so:

app/
├ js/
│ ├ gui/
│ │ ├ main.js
│ │ └ santa.js
│ └ app.js
└ index.html <- includes requirejs and starts js/app.js

app.js:

baseUrl: 'js' // Pseudo-code for requirejs.config() of course
require('gui/main');

So far so good. But every same-dir require() in main.js gives problems:

main.js:

require('santa')

Tries to require() app/js/santa.js which does not exist.

require('./santa')

Tries to require() app/js/santa.js which does not exist.

require('gui/santa')

Tries to require() app/js/gui/gui/santa.js which does not exist.

require('./gui/santa')

Tries to require() app/js/gui/gui/santa.js which does not exist.

wtf

The only way I seem to get my file to be included is like this:

require('js/gui/santa.js')

which kind of defeats the purpose of name resolving and the baseUrl setting.


Apart from this weirdness, I'd like to have the baseUrl changed to js/gui for any require() within main.js. The configuration is supposed to be extensible and overwritable. But when I do:

requirejs.config({
    baseUrl     : 'js/layout'
});

within main.js, the new setting seems to be ignored like politicians ignore their own promises.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Redsandro
  • 11,060
  • 13
  • 76
  • 106
  • Weird. Are you sure you put `require('./santa')` in your `gui/main.js` and not in your `app.js`? It should work just fine IIRC. – jgillich Mar 12 '14 at 15:05
  • 1
    Numbering your `require` attempts in `main.js` from 1 to 5, if 1 and 2 happen, then 3 and 4 are not possible *unless* you are passing to RequireJS a configuration that screws up its path resolution logic. (3 should resolve to the same as 1 but with the addition of a single `gui/` element in the path. 4 should resolve to the same as 2 with the same addition.) – Louis Mar 12 '14 at 16:23
  • Well, I don't, so there must be a bug in `RequireJS`. Opened an [issue](https://github.com/jrburke/requirejs/issues/1074). – Redsandro Mar 26 '14 at 21:04
  • What does your require do with filenames at a different subdirectory level? – Chris May 12 '14 at 15:22
  • It adds the subdirectory to the path twice when I request it. See the _app/js/gui/gui/santa.js_ example above. – Redsandro May 14 '14 at 23:50

1 Answers1

1

Have you tried using define instead of require?

Define can be called like this:

define('folder/main',
        [ "jquery", './AnotherModule' ],
        function($, AnotherModule) {});

The first parameter is the module name - an explicit path to the module. Normally, you wouldn't include the first parameter - define() always implicitly specifies a path, and in general using an explicit path is not recommended.

Require, on the other hand, does not take a name parameter. It's not possible to do a path relative to a source file with just require()'s and no define()'s, because require does not 'create' a module or define a namespace. Anything require()'d is required relative to root.

When you include a relative dependency in define() (like './AnotherModule'), it's found relative to the module name. In the above case, ./AnotherModule would resolve to folder/AnotherModule.

In a call to require(), there is no module name. Relative dependencies are resolved to the root.


In your case, this would account for the behavior of require('santa') and require('./santa'). My only guess as to why gui is not acting in the same manner is that, since it includes a subdirectory, it's handled differently. Try using require('strange-new-directory/santa') to see if you can get some more insight on the situation.


As for baseUrl resetting, baseUrl is, I believe, a global trait and can't be reset midstream (or at least, I wouldn't recommend doing it). You should be able to get relative pathing working with the use of define. Good luck.

Community
  • 1
  • 1
Chris
  • 5,876
  • 3
  • 43
  • 69
  • If I can just interchange the two, than my understanding of `requirejs` is incorrect or incomplete. The stuff that I `require` are modules that are `define`d themselves. As far as I know, this is how it is done, and also the way it should work if semantics are supposed to indicate proper usage. Or are you talking about some sort of trick/hack? – Redsandro May 14 '14 at 23:52
  • The difference between require and define has to do with how they specify determine pathing. I'll elaborate here but the linked SO question has the necessary information. – Chris May 15 '14 at 21:13