user3290525
user3290525

Reputation: 731

RequireJS not loading a file or module named "module.js"

I just started using RequireJS. I tried a simple code but one way works but the other doesn't.

folder "script" has "main.js", "module.js", "require.js"

<script data-main="script/main.js" src="script/require.js"></script>

in main.js

requirejs( ['module'], function( mod ) {
    mod.sayHello();
} );

in module.js:

define( {
    name : "value",
    sayHello : function() {
        alert( "Hello" );
    },
    sayBye : function() {
        alert( "Bye" );
    }
} );

I expect baseUrl to be "script" as mentioned in here:

http://requirejs.org/docs/api.html#jsfiles

The baseUrl is normally set to the same directory as the script used in a data-main attribute for the top level script to load for a page.

So, I thought there would be no problem, but doesn't mod.sayHello() nor sayBye() and console.log( mod.name ) = undefined.

I tried console.log( mod ) and it prints something like this:

Object {id: "_@r6", uri: "script/[email protected]", exports: Object}

When I use ["script/module.js"] instead of ["module"], console.log( mod ) prints like the following:

Object {name: "value"}
name: "value"
sayBye: ()
sayHello: ()
__proto__: Object

and mod.sayHello(), mod.sayBye(), mod.name all works.

including the following in the beginning of main.js is the same:

requirejs.config( {
    baseUrl: "script"
} );

What am I doing wrong... Please help.

Upvotes: 1

Views: 1065

Answers (2)

user3290525
user3290525

Reputation: 731

I continued reading the documentation:

http://requirejs.org/docs/api.html

and it led to a github that has information about this:

https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#magic

It turns out that "module" is a kind of "magic modules" along as "require", "export".

And "module" ... :

gives you information about the module ID and location of the current module

https://github.com/jrburke/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#module

Upvotes: 0

Louis
Louis

Reputation: 151370

Use a different name than module for your module. For one thing, it is a terribly uninformative name, but the module named module is a special module for RequireJS. It is a module that gives information about the module you are currently in. For instance if foo.js contains this code:

define(['module'], function (module) {
    console.log(module.id);
});

and this file is loaded when you request the module named foo, then console.log will show "foo" on the console.

The documentation does not highlight the existence of module but it talks about it when explaining what the configuration option config does. Because you get to access the config of your module through module.config().

The reason requiring "script/module.js" works is because when you do this you require a module named script/module.js rather than module.

Upvotes: 1

Related Questions