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


Groups > comp.lang.javascript > #28165

Re: Javascript Module

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Javascript Module
Date 2015-06-25 20:48 +0200
Organization PointedEars Software (PES)
Message-ID <10183241.khqZmVDl6i@PointedEars.de> (permalink)
References <8081bb27-e80e-40ce-b19f-4ca084324447@googlegroups.com> <558c0b0a$0$2954$e4fe514c@news2.news.xs4all.nl>

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web