ladar
ladar

Reputation: 5876

How to load custom named module in RequireJS

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...

Upvotes: 1

Views: 1029

Answers (1)

Trace
Trace

Reputation: 18859

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.

Upvotes: 1

Related Questions