Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28151 > unrolled thread

Javascript Module

Started bydc7669@hotmail.com
First post2015-06-24 06:43 -0700
Last post2015-06-25 16:47 +0200
Articles 4 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Javascript Module dc7669@hotmail.com - 2015-06-24 06:43 -0700
    Re: Javascript Module Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-06-25 16:07 +0200
      Re: Javascript Module Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-25 20:48 +0200
    Re: Javascript Module "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-06-25 16:47 +0200

#28151 — Javascript Module

Fromdc7669@hotmail.com
Date2015-06-24 06:43 -0700
SubjectJavascript Module
Message-ID<8081bb27-e80e-40ce-b19f-4ca084324447@googlegroups.com>
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/requirejs 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#notloaded"

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

Any help will be appreciated.  

Thanks.

[toc] | [next] | [standalone]


#28161

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2015-06-25 16:07 +0200
Message-ID<558c0b0a$0$2954$e4fe514c@news2.news.xs4all.nl>
In reply to#28151
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/requirejs 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#notloaded"
>
> 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.html#config

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

[toc] | [prev] | [next] | [standalone]


#28165

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-06-25 20:48 +0200
Message-ID<10183241.khqZmVDl6i@PointedEars.de>
In reply to#28161
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#notloaded"
>>
>> 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.html#config

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.html#add>
<http://requirejs.org/docs/whyamd.html#definition>

That said, this newsgroup is _not_ the RequireJS support.

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

[toc] | [prev] | [next] | [standalone]


#28163

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-06-25 16:47 +0200
Message-ID<mmh4a7$dbc$1@solani.org>
In reply to#28151
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#notloaded"
> 
> What did I do wrong?  [...]

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

-- 
Christoph M. Becker

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web