Search code examples
javascriptrequirejsamd

RequireJS absolute path produces Script Error


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.


Solution

  • 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
    ) { ...