8

I have a simple file which I'm loading dependencies with requireJS using define:

define([
    "dojo/_base/declare",
    "dojo/aspect",
    "local/path/to/myFile"
], function(
    declare,
    aspect,
    myFile
) { ...

This works as it should, the files are mapped in the requireJS config.

However, if I try to access one of the files using an absolute path (the exact same file):

define([
    "dojo/_base/declare",
    "dojo/aspect",
    "https://blah.com/absolute/path/to/myFile.js"
], function(
    declare,
    aspect,
    myFile
) { ... 

I get the following error:

"message": "Error: Script error for \"https://blah.com/absolute/path/to/myFile.js\", needed by: /home/test_vds_jasmine/test/modules/myFile-spec4.js\nhttp://requirejs.org/docs/errors.html#scripterror\nat /home/node_modules/requirejs/require.js:168:17\n\nmakeError@/home/node_modules/requirejs/require.js:168:17\nnewContext/context.onScriptError@/home/node_modules/requirejs/require.js:1738:36\n"

I've tried all manner of absolute path including with and without https, with and without the .js extension but am drawing a blank, the error message is not really helpful at all.

Am I calling the absolute path in the wrong way? Has anyone had experience doing it this way?

There's a valid reason why I have to call some files using absolute paths or I would just call all locally.

7
  • 1
  • Thanks @TarunLalwani will try those, thought I'd exhausted everything but the shim approach is interesting
    – StudioTime
    Commented Apr 8, 2018 at 8:54
  • 1
    @DarrenSweeney What's the result of the network request for loading that file? In the debugger, check both the HTTP status on the response and the contents actually transmitted (which could be different from the content you expect). If the network request fails or you get the wrong content then there's nothing RequireJS can do to fix that, and the issue is elsewhere.
    – Louis
    Commented Apr 8, 2018 at 15:33
  • 1
    @DarrenSweeney show us the define statement of the loaded file - and the message of the actual exception - Also are there any baseUrl/path definitions in your setup? They will be applied differently if loaded via https... So dependencies will be resolved using a different strategy.
    – Sebastian
    Commented Apr 8, 2018 at 16:09
  • 1
    @DarrenSweeney, any update on the link I posted, does it help or not? Commented Apr 14, 2018 at 5:36

2 Answers 2

6
+200

Try this, put your URL in your requirejs config like this:

requirejs.config({
        paths: { 'myFileRemote': 'https://blah.com/absolute/path/to/myFile.js' }
    });

Now update your file with above mapping:

define([
    "dojo/_base/declare",
    "dojo/aspect",
    "myFileRemote"
], function(
    declare,
    aspect,
    myFile
) { ... 
1

Suggested Solution

Your myFile.js should have a define block to be used like that. Upgrade your myFile.js with define structure.

myFile.js:

define(["./cart", "./cart2"], function(cart, cart2) {
    return {
        color: cart.color,
        size: "large",
        addToCart: function() {
        }
    }
});

Alternative Solution

If you cannot modify myFile.js to that structure just add it to the path of requirejs config the way you add jQuery. You can add the config at the top of myFile.js or app.js (the file you are calling myFile.js from). If you add config to app.js, you can access it from everywhere.

myFile.js/app.js:

require.config({
    paths: {
        "myFile": "https://blah.com/absolute/path/to/myFile"
    }
});

Then you can use it as follows:

myFile.js:

define([
    "dojo/_base/declare",
    "dojo/aspect",
    "myFile"
], function(
    declare,
    aspect,
    myFile
) { ...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.