[SOLVED] Importing NakamaJs module

My Code:

var ItsAscript = pc.createScript('itsAscript');

ItsAscript.prototype.initialize = function() {
    var nakamajs = document.createElement('script');
    nakamajs.src = '/nakama-js.umd.js';

    var client = new nakamajs.Client("defaultkey", "", 7350);
    client.ssl = false;
    print("print ho rha hai");
    var email = "super@heroes.com";
    var password = "batsignal";
    var session = client.authenticateEmail({ email: email, password: password });

// update code called every frame
ItsAscript.prototype.update = function(dt) {

//const client = new nakamajs.Client("defaultkey");

I’m trying to import NakamaJs module but I keep getting this error:

[ItsAScript.js?id=16996646&branchId=5a76c294-f453-41ae-be63-d7b54f2861dd:15]: nakamajs.Client is not a constructor

TypeError: nakamajs.Client is not a constructor

I’m also trying to use Nakama with PlayCanvas, if anyone has any suggestions that would be welcome.

I’ve just put nakama-js.umd.js in the scripts folder, and then in my other .js script in the initialize() function, I use OP’s code to import Nakama (just the 3 lines of code in his post), but the Chrome debug log shows:
Failed to load resource: the server responded with a status of 404 (Not Found)

EDIT: Nakama’s documentaion (https://heroiclabs.com/docs/javascript-client-guide/) explains that the only requirement is to include nakama-js.umd.js in the script html tag. So I’m about to search the forums for how to do that.

If you have nakama-js.umd.js in the project, then it’s automatically added to the header. Just make sure that it is first in the script loading order in the project settings.

Thank you @yaustar. This has worked for me.

Did the script loading changed in a year? I tried with the example and it throws a 404.

Not as far as I know, can you share an example project of the issue please?


We were experiencing the same issue as the code does indeed 404. It looks like the following line is the culprit:

nakamajs.src = ‘/nakama-js.umd.js’;

I think this defaults to the root and looking at where other scripts are located this seems to be wrong. My guess is it should be something like the following but we actually fixed it an alternative way so can’t confirm this:

nakamajs.src = ‘/api/assets/files/scripts/nakama-js.umd.js’

So how did we fix it?

What we actually did to remedy this was to remove the three lines where it creates the script tag for Nakama-js.umd.js and adds it to the head. Now obviously the script is no longer in the header so to get it into the header we went to the Nakama-js.umd.js file in the editor and in the inspector the is a drop down under “Loading Type”. By changing this to “Before Engine” actually adds this necessary file to the header for you thus no longer requiring the code to add it manually. This resolved the 404 error for us.


Side note: If you need to get the URL to an asset like a JS or an image that is in the project, you can use getFileUrl and that will work on both builds and in the launch tab: https://developer.playcanvas.com/en/api/pc.Asset.html#getFileUrl

1 Like

You don’t need to import nakama client as a src. when you put the script as #1 in the “script load order” section nakamajs.client is available by default from any script. so you only need to create a object reference for it.

client = new nakamajs.Client("defaultkey", "", "7350");

The script asset works for me when is configured as “asset” “preload” and is in the first items in the load order array.

1 Like