[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Javascript Module

dc7669

6/24/2015 1:43:00 PM

I am new to Javascript and trying to learn about modules.

I have this greetings-main.htm:
<html>
<script src="./require.js"> </script>

<script>


// main.js
var greetings = require("./greetings.js");

// "Hello"
greetings.sayHelloInEnglish();

// "Hola"
function Test(){
greetings.sayHelloInSpanish();
}

console.log(Test());
console.log('12345967890');

</script>

</html>

greetings.js
module.exports = {
sayHelloInEnglish: function() {
return "HELLO";
},
sayHelloInSpanish: function() {
return "Hola";
}
};

I got require.js. This is the information about require.js
/** vim: et:ts=4:sw=4:sts=4
* @license RequireJS 2.1.18 Copyright (c) 2010-2015, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke... for details
*/


The problem is that when I run greetings-main.htm, I got an error saying, "require.js:166 Uncaught Error: Module name "greetings.js" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notlo...

What did I do wrong? greetings.js file exists in the same directory of greetings-main.htm.

Any help will be appreciated.

Thanks.
3 Answers

Erwin Moller

6/25/2015 2:07:00 PM

0

On 6/24/2015 3:43 PM, dc7669@hotmail.com wrote:
> I am new to Javascript and trying to learn about modules.
>
> I have this greetings-main.htm:
> <html>
> <script src="./require.js"> </script>
>
> <script>
>
>
> // main.js
> var greetings = require("./greetings.js");
>
> // "Hello"
> greetings.sayHelloInEnglish();
>
> // "Hola"
> function Test(){
> greetings.sayHelloInSpanish();
> }
>
> console.log(Test());
> console.log('12345967890');
>
> </script>
>
> </html>
>
> greetings.js
> module.exports = {
> sayHelloInEnglish: function() {
> return "HELLO";
> },
> sayHelloInSpanish: function() {
> return "Hola";
> }
> };
>
> I got require.js. This is the information about require.js
> /** vim: et:ts=4:sw=4:sts=4
> * @license RequireJS 2.1.18 Copyright (c) 2010-2015, The Dojo Foundation All Rights Reserved.
> * Available via the MIT or new BSD license.
> * see: http://github.com/jrburke... for details
> */
>
>
> The problem is that when I run greetings-main.htm, I got an error saying, "require.js:166 Uncaught Error: Module name "greetings.js" has not been loaded yet for context: _. Use require([])
> http://requirejs.org/docs/errors.html#notlo...
>
> What did I do wrong? greetings.js file exists in the same directory of greetings-main.htm.
>
> Any help will be appreciated.
>
> Thanks.
>

Have you checked your path(s) in require.config?
http://requirejs.org/docs/api.h...

Also, but that is not your problem, your functions return a string, for
example:
sayHelloInSpanish: function() {
return "Hola";
}

So you must do something with that, like console.log() or alert() when
you call them like you do:
function Test(){
greetings.sayHelloInSpanish();
}

should probably be:

function Test(){
alert(greetings.sayHelloInSpanish());
}


Regards,
Erwin Moller


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens

Christoph M. Becker

6/25/2015 2:48:00 PM

0

dc7669@hotmail.com wrote:

> The problem is that when I run greetings-main.htm, I got an error saying, "require.js:166 Uncaught Error: Module name "greetings.js" has not been loaded yet for context: _. Use require([])
> http://requirejs.org/docs/errors.html#notlo...
>
> What did I do wrong? [...]

Apparently, you have not read the document that is mentionend in the
error message.

--
Christoph M. Becker

Thomas 'PointedEars' Lahn

6/25/2015 6:49:00 PM

0

Erwin Moller wrote:

> On 6/24/2015 3:43 PM, dc7669@hotmail.com wrote:
>> I am new to Javascript and trying to learn about modules.
>> [â?¦]
> <script src="./require.js"> </script>

The â??data-mainâ? attribute is missing here, see below.

>> // main.js
>> var greetings = require("./greetings.js");
>> [â?¦]
>>
>> greetings.js
>> module.exports = {
>> sayHelloInEnglish: function() {
>> return "HELLO";
>> },
>> sayHelloInSpanish: function() {
>> return "Hola";
>> }
>> };
>>
>> [â?¦]
>> The problem is that when I run greetings-main.htm, I got an error
>> saying, "require.js:166 Uncaught Error: Module name "greetings.js" has
>> not been loaded yet for context: _. Use require([])
>> http://requirejs.org/docs/errors.html#notlo...
>>
>> What did I do wrong? greetings.js file exists in the same directory of
>> greetings-main.htm.
>
> Have you checked your path(s) in require.config?
> http://requirejs.org/docs/api.h...

Configuration of paths is unnecessary in this case (so is writing the â??.jsâ?
suffix). However, the unnecessary configuration could be erroneous.

> Also, but that is not your problem, your functions return a string, [â?¦]
>
> So you must do something with that, like console.log() or alert() [â?¦]

I do not think any of this is going to help much. The OP appears to have
confused Node.js modules (as indicated by the â??module.exportsâ?) with
CommonJSâ??s Asynchronous Module Definition (AMD) modules.

RequireJS explicitly supports only the latter approach, and it says so in
the documentation. That is why it complains that no such module has been
loaded â?? there is *no* module that it can load.

<http://requirejs.org/docs/start.ht...
<http://requirejs.org/docs/whyamd.html#defi...

That said, this newsgroup is _not_ the RequireJS support.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.