Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ratchet / When.js: "Uncaught ReferenceError: module is not defined"

I'm trying to use ratchet for reflecting the changes made to my database realtime. I used the code of the pusher app (http://socketo.me/docs/push). But i am getting error in that.

The moment I open the client side page :

Uncaught ReferenceError: module is not defined                    when.js:900
(anonymous function)                                              when.js:900
(anonymous function)                                              when.js:15
(anonymous function)                                              when.js:900

After this I typed: conn.subscribe('topic'); //This subscribes the topic.

Now the moment I make changes to this topic, an error pops up where the changes are to be displayed.

Error 2 :

Uncaught TypeError: undefined is not a function         [VM] autobahn.min.js (124):66
(anonymous function)                                    [VM] autobahn.min.js (124):66
c._websocket.onmessage                                  [VM] autobahn.min.js (124):66

Any help ?

like image 271
Ashish Avatar asked Sep 06 '13 20:09

Ashish


2 Answers

This may be solvable by using the following JavaScript before including the when.js file:

window.define = function(factory) {
    try{ delete window.define; } catch(e){ window.define = void 0; } // IE
    window.when = factory();
};
window.define.amd = {};

Then, include your when.js file.

like image 82
Jimbo Avatar answered Sep 19 '22 05:09

Jimbo


This is because you're using the when.js without using a module system such as AMD.

The docs explain how to transform the file to work in a browser through use of browserify, so long as you've done n

Browser environments (via browserify)

Since when.js primarily targets modular environments, it doesn't export to the global object (window in browsers) by default. You can create your own build of when.js using browserify, if you prefer not to use an AMD or CommonJS loader in your project.

  1. git clone https://github.com/cujojs/when
  2. npm install
  3. npm run browserify to generate build/when.js
    1. Or npm run browserify-debug to build with when/monitor/console enabled
  4. <script src="path/to/when/build/when.js"></script>
    1. when will be available as window.when
    2. Other modules will be available as sub-objects/functions, e.g. window.when.fn.lift, window.when.sequence. See the full sub-namespace list in the browserify build file
like image 37
Incognito Avatar answered Sep 19 '22 05:09

Incognito