GitHub | Documentation | Discord (chat) | Become a Patron

[Bug] Uncaught TypeError: Colyseus.Client is not a constructor



  • I have been using colyseus very well for a long time in Cocos Creator by importing dist/colyseus.js in my project. But recently when I updated it, both server and client, the client gave me error:

    Uncaught TypeError: Colyseus.Client is not a constructor

    And my code is the same with offical document:

    import * as Colyseus from "colyseus.js";
    var client = new Colyseus.Client('ws://localhost:2657');

    I checked file dist/colyseus.js and found it has changed a lot, which is seemed to be compiled and in just one line. The change ID is c4063f63ebfc4aadf39254d611ae8c81406ac203.

    I want to use consent argument in sever side for callback onLeave() so I have to update it.

    Can anyone help me? Thanks very much!


  • administrator

    Hi @Michaelbest1

    The hash you provided seems to be from version 0.8.x. I'd recommend upgrading your server-side along with it to the latest version (colyseus@^0.9.33)

    How's it possible that you use the dist/colyseus.js file and still have an import statement?

    import * as Colyseus from "colyseus.js";

    AFAIK, if you're using the dist file, you should be able to use it without importing at all, because it's already in the global scope.

    Cheers



  • Hi @endel
    Thanks for your replay. Of course I've updated both server and client to latest version. The hash I shown is just the version that the dist file changed.
    I'm sorry that I'm not professional on JS. I've tried import, require and without any import but none of them works. If I use this:

    var Colyseus = require("colyseus");

    The error message is:

    ColyClient.js:16 Uncaught TypeError: Cannot read property 'Client' of undefined

    And if without any importing, the error message is:

    Colyseus is not defined

    The runtime of cocos creator is different from web browser. I think there is no global scope at all. If you want to use any module in js or ts, you have to import it or require it.
    So could you try cocos creator and find a solution? Thanks very much!


  • administrator

    Hi @Michaelbest1!

    I think I've just fixed this by using umd instead of var to generate the colyseus.js dist file: https://github.com/colyseus/colyseus.js/tree/master/dist

    Can you check if it works for you now?

    I'm finding Cocos Creator a bit confusing, I'll check here if I can test here too.

    Cheers



  • @endel cocoscreator use CommonJs



  • Hi @endel
    It WORKS! Thanks a lot!
    I learned just now that there're several ways of packing js. And maybe umd is suitable for CommonJS mentioned by @parkcd. So please use this way for future pack : )