1

I know it is recommended not to use them, but let's say just for fun I'd like to use module with custom name. How can I load it?

I have following structure:

-- ./index.html
-- ./js/app.js
-- ./js/test.js

In HTML, I'm loading RequireJS (2.1.14)

<script src="js/require.js" data-main="js/app" type="text/javascript"></script>

In app.js:

require(["dummy"], function(){
    window.console.log("ready");  
})

In test.js:

define("dummy", [], function(){
    window.console.log("dummy loaded");
})

But RequireJS is trying to load dummy.js. What am I missing here?

Update: I know I can use require.config to load the file

require.config({
    paths: {
        "dummy" : "test"
    }
})

But then I don't understand why is one able to define custom name if he has to re-declare it again in paths...

1 Answer 1

1

I think you need to define this in your config (app.js) as a property of the 'paths' object:

require.config({
    paths: {
        dummy: 'libs/whatever'
    }
});  

Edit

A few notes:

In test.js, I think that you don't need to add the empty array if you don't require other modules.
In app.js, you didn't add "dummy" as function argument.

I suspect that requirejs expects you to define a return value from the module definition.

AMD = Asynchronous Module Definition

I don't think that there is a reason to use the 'define' and 'require' methods if you are not using these modules for asynchronous dependency management, rather than for executing a script.

Sign up to request clarification or add additional context in comments.

6 Comments

I know I can use this, but then I don't really have to use named module, right? I can declare this way and "rename" any module.
This is, if I understand it correctly, the reason custom names are not recommended. The issue is that, without the require.config setup, in asynchronous mode RequireJS has no way of knowing where dummy maps to - so it falls back on it's default algorithm (basePath + module/id). When you build your modules (with r.js or similar) this is not a problem because both modules are loaded in the bundle.)
@KimGysen Thanks but I don't think that the notes are important in this case. This is a dummy useless case, just to know how it works. It just seems redundant to use custom module name if you'd have to declare the name again in require.config's path property. Then I can skip module name at all and use just path property.
For me personally it depends; I only use 'paths' for modules I use repetitively again. For modules I need only once, I just require the path. If I'm working with other persons on a project, I wouldn't feel comfortable requiring modules when there's no visible path towards the required module (just a definition in a module 'somewhere' where I chose, but others don't know). I understand from the point of view from playing around with the library though.
I agree with you. I don't want to use named module, I was just wondering that the documentation mentions it but I can't find and working example how to use 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.