14

I have just started using RequireJS to load the scripts for my page, but have hit a roadblock when trying to debug a problem using chrome.

Let's say that I have my main script that relies on two dependencies: dep1 and dep2

require(["dep1", "dep2"], function(dep1, dep2) {
    //do something
});

Whilst running unit tests I notice that something in dep1.js isn't working correctly, so I open up chrome dev tools to insert a breakpoint, except...

missing dependencies

...dep1.js doesn't exist!

How do I locate the source file to insert my breakpoint?!

I am sure that the files are being loaded as the scripts are being run correctly. I just can't see them in the list of sources

2
  • 3
    @downvoter - any comment/reason for downvoting? Commented Jan 16, 2013 at 20:20
  • Another way to verify / access the loaded sources is via the Network tab in Chrome Developer Tools. You can right-click on the source to view it. Commented Nov 17, 2015 at 16:33

2 Answers 2

4

Finally realised the problem was that I was using r.js instead of require.js. As soon as I switched I could see the files without a problem.

As an aside, a temporary work-around was to insert the line below at the end of the inserted files. This just about gets chrome to pick up on the files.

//@ sourceURL=GameWorldAction.js
Sign up to request clarification or add additional context in comments.

3 Comments

Quite interesting this was my suggestion one month before you posted this (see below), but not a single upvote..
@RobbertvandenBogerd I don't see any answer besides the one from jeremyawesome. If you mean your comment on that answer then yes, that's true - you should have posted it as a separate answer!
@RobbertvandenBogerd +1 upboats from the future!
1

You never close your array in your example above.

require(["dep1", "dep2", function(dep1, dep2) {
    //do something
});

Should be:

require(["dep1", "dep2"], function(dep1, dep2) {
    //do something
});

Edit in Response to Update to Question:

I am assuming r.js is the RequireJs file. It looks like RequireJS is not loading the files correctly, if it had then the files would've shown up in your inspector.

Are you using the data-main attribute on your script tag to specify a main JS file? If so, are the dep1 and dep2 files in the same directory as the main JS file? You might have to specify a different baseUrl if you are trying to load files from a different path or domain. You can change the baseUrl by setting it in the require.config.

<script>
  require.config({
    //path can also be a domain like "//image.mydomain.tld/jscript"
    baseUrl: "/another/path" 
  });
</script>

5 Comments

The example was typed directly into SO - the array is closed in the real code. I've updated the question
Yes, I am using the data-main attribute and the files are definitely loading correctly (I can see that because my tests are running). I am actually specifying a separate baseUrl as well as some path mappings but I don't think that's the problem
I hadn't, but (at first glance) that's about scripts disappearing rather than never appearing in the first place. I'll read through though
@jeremysawesome your assumption is wrong, r.js is the requireJS optimizer
@RobbertvandenBogerd really it is? Are you sure it's not another random JavaScript file named "r.js"? You can't be sure, you must be assuming that the r.js file is the optimizer. The point is: if r.js was not the RequireJS file then the OP was not including a necessary file. However, in the future, rather than assuming, I will simply ask "are you including the require.js file? Because the screenshot doesn't show it."

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.