[SOLVED] How do i import an es6 module?

hello, i need to do some like this:

import NakamaJs from "./pc-nakama.min.js";
var nakama = new NakamaJs(param1, param2, param3, param4, param5);

using the engine-only works but i need to import my Module inside a Playcanvas script or find a way to load it.

[Solution]:

Instead use the name of the object (‘NakamaJS’) use ‘module.default’

Example:

var module;

(function() {
    var asset = pc.Application.getApplication().assets.find('pc-nakama.min.js');
    
    async function load() {
        module = await import(asset.getFileUrl());
        var nakama = new module.default(params, params..);
    }
    
    load();
})();
1 Like

If you are using engine only, you can import it in a way that the browser supports it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#applying_the_module_to_your_html

There shouldn’t be anything PlayCanvas specific needed.

yeah it works, but i need to apply it using the editor. I tried something like, but it tells me that the object i want to construct (NakamaJS) is not defined:

(function() {
    var asset = pc.Application.getApplication().assets.find('pc-nakama.min.js');
    async function load() {
        let module = await import(asset.getFileUrl());
        //client = new nakamajs.Client("defaultkey", "134.209.219.5", "7350");
        var nakama = new NakamaJs(param, param, param, param, param);
    }
    
    load();
})();

In that code, check what module variable is in the debugger. I’ve not used modules in the Editor before but I think that contains the module objects/functions etc.

1 Like