Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 462
StudioTime Avatar asked Apr 05 '18 10:04

StudioTime


2 Answers

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
) { ... 
like image 79
Navjot Ahuja Avatar answered Oct 12 '22 11:10

Navjot Ahuja


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
) { ...
like image 28
Munim Munna Avatar answered Oct 12 '22 10:10

Munim Munna