i have a script which i want to expose a function using JS Modules this script is compiled using esbuild.
pre-transpiled file (index.mjs)
const NakamaWrapper = require("./nakama").default
var NakamaJS;
export default function InitNakama(host, port, useSSL) {
NakamaJS = new NakamaWrapper(host, port, useSSL);
NakamaJS.initiate();
}
esbuild task
"build-dev": "./node_modules/.bin/esbuild ./src/index.mjs --bundle --sourcemap --target=es2015 --outfile=./dist/dev/pc-nakama.js",
exported code (i notice the function is inside an IIFE)
code in the html:
import * as NakamaJS from "./pc-nakama.js";
NakamaJS.InitNakama("192.168.100.50", 7350, false);
error:
NakamaJS.InitNakama is not a function
The output format defaults to
--format=iife
but you can use--format=esm
to output in ECMAScript module format instead, which will work withimport
. Docs are here: https://esbuild.github.io/api/#format-esm.