admin管理员组文章数量:1399935
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",
".js"
], function(
declare,
aspect,
myFile
) { ...
I get the following error:
"message": "Error: Script error for \".js\", needed by: /home/test_vds_jasmine/test/modules/myFile-spec4.js\n.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.
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./absolute/path/to/myFile.js"
], function(
declare,
aspect,
myFile
) { ...
I get the following error:
"message": "Error: Script error for \"https://blah./absolute/path/to/myFile.js\", needed by: /home/test_vds_jasmine/test/modules/myFile-spec4.js\nhttp://requirejs/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.
Share Improve this question edited Apr 8, 2018 at 8:37 StudioTime asked Apr 5, 2018 at 10:52 StudioTimeStudioTime 24.1k40 gold badges128 silver badges215 bronze badges 7- 1 See if these help stackoverflow./questions/21624503/…, stackoverflow./questions/9759151/…, coderwall./p/y4vk_q/requirejs-and-external-scripts – Tarun Lalwani Commented Apr 8, 2018 at 8:45
-
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? – Tarun Lalwani Commented Apr 14, 2018 at 5:36
2 Answers
Reset to default 6 +200Try this, put your URL in your requirejs config like this:
requirejs.config({
paths: { 'myFileRemote': 'https://blah./absolute/path/to/myFile.js' }
});
Now update your file with above mapping:
define([
"dojo/_base/declare",
"dojo/aspect",
"myFileRemote"
], function(
declare,
aspect,
myFile
) { ...
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./absolute/path/to/myFile"
}
});
Then you can use it as follows:
myFile.js:
define([
"dojo/_base/declare",
"dojo/aspect",
"myFile"
], function(
declare,
aspect,
myFile
) { ...
本文标签: javascriptRequireJS absolute path produces Script ErrorStack Overflow
版权声明:本文标题:javascript - RequireJS absolute path produces Script Error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744167335a2593612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论