1

Since most issues with require.js build has to do with file structure and relative path reference, I created a repo here: https://github.com/ttback/requirejs-example for easier troubleshoot.

The error is when i run grunt, I will get no such file or directory requirejs-example/src/js/bundle/js/bundle/utils.js

This is due to the wrong baseUrl. I want it to be src/ but I can't set it since it goes to find the dependencies for src/js/bundle/main.js based on my Gruntfile. So the base is at src/js/bundle. The current main.js works with the index.html, if I change the relative path to utils.js from .js/bundle/utils.js to ./utils.js inside main.js, the app wil break.

Is there any way I can make the grunt-requirejs work with what I have?

ttback
  • 2,051
  • 5
  • 27
  • 40

1 Answers1

2

First of all, points for adding a decent code example.

The problem is easy to solve actually.

Simply change:

var utilsObject = require('./js/bundle/utils.js');

to:

var utilsObject = require('./utils');

Now the build tool and the app will work.

By adding a .js suffix you were bypassing the baseUrl rules that RequireJS applies to module paths. From the docs:

RequireJS also assumes by default that all dependencies are scripts, so it does not expect to see a trailing ".js" suffix on module IDs.

There may be times when you do want to reference a script directly and not conform to the "baseUrl + paths" rules for finding it. If a module ID has one of the following characteristics, the ID will not be passed through the "baseUrl + paths" configuration, and just be treated like a regular URL that is relative to the document:

  • Ends in ".js".
  • Starts with a "/".
  • Contains an URL protocol, like "http:" or "https:".

http://requirejs.org/docs/api.html#jsfiles

Community
  • 1
  • 1
Simon Smith
  • 8,024
  • 2
  • 30
  • 40
  • thanks, but that's the first thing I tried. What happens if you change it and run the app as-is, it won't be able to find utils. (The build tool will work and generate a build.js) If I wire build.js by doing , the app doesn't do anything....Do I need some extra code to invoke the module? – ttback Nov 22 '13 at 15:54
  • I changed that one line and ran the app in the browser. All files loaded correctly (I saw the alert) and the build tool worked. You said you tried it with the .js extension, did you try that without? – Simon Smith Nov 22 '13 at 16:20
  • i see...hmmm, will try that again, interesting point on the suffix – ttback Nov 24 '13 at 18:23
  • i see, so in order to not make it going to treat it as a relative path, but go through requirejs's baseURL setting, I have to not use .js...interesting point. thanks. This should solve most of my problems to make it work with both r.js and without. Do you know why when I just load the build.js (replace main.js with it), it won't do anything? The document.ready code doesn't seem t be executed, do I have to take it out of the main module? – ttback Nov 24 '13 at 18:28