# GoError: Failed to find InitModule function when using registerMatch

**URL:** <https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129>\
**Category:** Local Setup\
**Tags:** getting-started, server-framework, typescript\
**Created:** [October 4, 2022, 3:50pm UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129 "2022-10-04T15:50:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AngelMashiro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/angelmashiro/32/1363_2.png) [@AngelMashiro](https://forum.heroiclabs.com/u/AngelMashiro)\
**Post date:** [October 4, 2022, 3:50pm UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/1 "2022-10-04T15:50:56Z")

</div>

I just started working on the server side things of Nakama and wanted to make use of the matchLoop for my game in Unity. Looking at the documentation I saw that I had to use initializer.registerMatch to do that. I defined the needed functions in a separate file called “match\_handler.ts”. After starting the server it gives the following errors:

```auto
server-nakama-1 | {"level":"error","ts":"2022-10-04T15:22:43.334Z","caller":"server/runtime_javascript.go:1601","msg":"Failed to eval JavaScript modules.","error":"GoError: failed to find InitModule function\n\tat github.com/heroiclabs/nakama/v3/server.(*RuntimeJavascriptInitModule).registerMatch.func1 (native)\n\tat InitModule (index.js:11:9(23))\n\tat native\n"}
server-nakama-1 | {"level":"error","ts":"2022-10-04T15:22:43.334Z","caller":"server/runtime.go:614","msg":"Error initialising JavaScript runtime provider","error":"GoError: failed to find InitModule function\n\tat github.com/heroiclabs/nakama/v3/server.(*RuntimeJavascriptInitModule).registerMatch.func1 (native)\n\tat InitModule (index.js:11:9(23))\n\tat native\n"}
server-nakama-1 | {"level":"fatal","ts":"2022-10-04T15:22:43.334Z","caller":"main.go:158","msg":"Failed initializing runtime modules","error":"GoError: failed to find InitModule function\n\tat github.com/heroiclabs/nakama/v3/server.(*RuntimeJavascriptInitModule).registerMatch.func1 (native)\n\tat InitModule (index.js:11:9(23))\n\tat native\n"}

```

It’s weird that it says that it failed to find the initModule, as I have defined that in my main.ts file, and it works when I remove the registerMatch call. I’ve tried many different examples and looking at existing repo’s but just can’t seem to fix it.

1. Versions: Nakama 3.13.1, Windows/Docker
2. Server Framework Runtime language (If relevant): TS/JS

match\_handler.ts:

```auto
{// Match initialization function (runs once when the match is created either via rpc or through the matchmaker)
const matchInit: nkruntime.MatchInitFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, params: {[key: string]: string}) : {state: nkruntime.MatchState, tickRate: number, label: string} {
    logger.debug('Match initialized.');
    return {
            state: { },
            tickRate: 1,
            label: ''
    };
};

// When a player tries to join
const matchJoinAttempt: nkruntime.MatchJoinAttemptFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence, metadata : {[key : string]: any}) : {state: nkruntime.MatchState, accept: boolean } | null {        
    return {
        state,
        accept: true
    };
};

// When one (or multiple) player(s) actually join(s)
const matchJoin: nkruntime.MatchJoinFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : {state: nkruntime.MatchState} | null {
    return {state};
};

// When a player leaves
const matchLeave: nkruntime.MatchLeaveFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, presences: nkruntime.Presence[]) : {state: nkruntime.MatchState} | null {
    return {state};
};

// Runs every tick
const matchLoop: nkruntime.MatchLoopFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, messages: nkruntime.MatchMessage[]) : {state: nkruntime.MatchState} | null {
    return {state};
};

// Runs when the match gets terminated
const matchTerminate: nkruntime.MatchTerminateFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, graceSeconds: number) : {state: nkruntime.MatchState} | null {
    return {state};
};

const matchSignal: nkruntime.MatchSignalFunction<nkruntime.MatchState> = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, dispatcher: nkruntime.MatchDispatcher, tick: number, state: nkruntime.MatchState, data: string) : {state: nkruntime.MatchState, data?: string} | null {
    logger.debug('Lobby match signal recieved: ' + data);
    return {state, data};
};}

```

Main.ts:

```auto
const InitModule: nkruntime.InitModule = function(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
    logger.debug("Hello World!!@!!");

    initializer.registerMatch('match', {
        matchInit,
        matchJoinAttempt,
        matchJoin,
        matchLeave,
        matchLoop,
        matchSignal,
        matchTerminate
      });
}

```

---

<div class="post-metadata">

**Author:** ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)\
**Post date:** [October 4, 2022, 5:02pm UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/2 "2022-10-04T17:02:59Z")

</div>

Hey @AngelMashiro are you adding your new file to the `files` section of your `tsconfig.json` as shown here in this example: [Nakama: TypeScript Runtime | Heroic Labs Documentation](https://heroiclabs.com/docs/nakama/server-framework/typescript-runtime/#develop-code)

---

<div class="post-metadata">

**Author:** ![AngelMashiro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/angelmashiro/32/1363_2.png) [@AngelMashiro](https://forum.heroiclabs.com/u/AngelMashiro)\
**Post date:** [October 4, 2022, 5:09pm UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/3 "2022-10-04T17:09:40Z")

</div>

Hey, I have added both my files to my tsconfig.json, it looks as followed:

```auto
{
  "files": [
    "./main.ts",
    "./match_handler.ts",
  ],
  "compilerOptions": {
    "target": "es2015", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "typeRoots": [
      "./node_modules"
    ], 
   "outFile": "./build/index.js",
    "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
   "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
    "strict": true, /* Enable all strict type-checking options. */
    "skipLibCheck": true /* Skip type checking all .d.ts files. */
  }
}

```

The filenames in the config match those that exist in the folder

---

<div class="post-metadata">

**Author:** ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)\
**Post date:** [October 4, 2022, 5:13pm UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/4 "2022-10-04T17:13:57Z")

</div>

Okay, if you run `npx tsc` you should see that your `index.js` file has updated. Do you still hit the same error?

---

<div class="post-metadata">

**Author:** ![AngelMashiro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/angelmashiro/32/1363_2.png) [@AngelMashiro](https://forum.heroiclabs.com/u/AngelMashiro)\
**Post date:** [October 4, 2022, 5:26pm UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/5 "2022-10-04T17:26:19Z")

</div>

I use Docker to run the server, and the Dockerfile compiles the typescript files. But if I manually run the command I get a bunch of errors (71 to be exact) that it can’t find a definition file, example of last 4 being:

```auto
error TS2688: Cannot find type definition file for 'type-check'.
  The file is in the program because:
    Entry point for implicit type library 'type-check'

error TS2688: Cannot find type definition file for 'which'.
  The file is in the program because:
    Entry point for implicit type library 'which'

error TS2688: Cannot find type definition file for 'wrappy'.
  The file is in the program because:
    Entry point for implicit type library 'wrappy'

error TS2688: Cannot find type definition file for 'yallist'.
  The file is in the program because:
    Entry point for implicit type library 'yallist'

```

None of which I even use, but I assume that’s what Nakama uses.

In case it’s relevant, this is the dockerfile that I normally use to start the server:

```auto
FROM node:alpine AS node-builder

WORKDIR /backend

COPY package*.json .
RUN npm install

COPY tsconfig.json .
COPY *.ts .
RUN npx tsc

FROM registry.heroiclabs.com/heroiclabs/nakama:3.13.1

COPY --from=node-builder /backend/build/*.js /nakama/data/modules/build/
COPY local.yml .

```

---

<div class="post-metadata">

**Author:** ![AngelMashiro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/angelmashiro/32/1363_2.png) [@AngelMashiro](https://forum.heroiclabs.com/u/AngelMashiro)\
**Post date:** [October 5, 2022, 8:39am UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/6 "2022-10-05T08:39:52Z")

</div>

I tried manually compiling the files as you asked me to. I hadn’t installed nakama-common yet.  
On the [Typescript runtime page](https://heroiclabs.com/docs/nakama/server-framework/typescript-runtime/) it says to install it with `npm i 'https://github.com/heroiclabs/nakama-common'`, but that didn’'t work for me. It gave me an error that that repository doesn’t have a package.json file:

```auto
npm ERR! Could not install from "'https:\github.com\heroiclabs\nakama-common'" as it does not contain a package.json file.

```

So I installed it using `npm install heroiclabs/nakama-common`, which I believe achieves the same thing.

I don’t get errors when manually compiling it now, and I can see the index.js file in my build folder update. I still get the same error as before however.

---

<div class="post-metadata">

**Author:** ![lugehorsam](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lugehorsam/32/314_2.png) [@lugehorsam](https://forum.heroiclabs.com/u/lugehorsam)\
**Post date:** [October 5, 2022, 12:37pm UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/7 "2022-10-05T12:37:27Z")

</div>

Hey @AngelMashiro what version of node and npm are you on?

---

<div class="post-metadata">

**Author:** ![AngelMashiro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/angelmashiro/32/1363_2.png) [@AngelMashiro](https://forum.heroiclabs.com/u/AngelMashiro)\
**Post date:** [October 6, 2022, 3:29pm UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/8 "2022-10-06T15:29:46Z")

</div>

Hey, I was running an older version of node (v10.x). I just realised I missed the part where it said the prerequisite was Node v14 or later. I updated my node (with neccesary tools) and npm and that seemed to have fixed it. Thank you for pointing it out!

---

<div class="post-metadata">

**Author:** ![LucaJeevanjee](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/lucajeevanjee/32/1793_2.png) [@LucaJeevanjee](https://forum.heroiclabs.com/u/LucaJeevanjee)\
**Post date:** [September 9, 2023, 2:46pm UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/9 "2023-09-09T14:46:51Z")

</div>

Hi, don’t know if I’m too late but I am also getting this error.  
My app has the same logs when running `docker compose up` where no modules are found  
I am using typescript with node 19.7 and nakama common 1.28.1 and nakama 3.17.1 which all seem to be new.

My dockerfile

```auto
FROM node:alpine AS node-builder

WORKDIR /backend

COPY package*.json .
RUN npm install

COPY tsconfig.json .
COPY main.ts .
RUN npx tsc

FROM registry.heroiclabs.com/heroiclabs/nakama:3.17.1

COPY --from=node-builder /backend/build/*.js /nakama/data/modules/build/
COPY local.yml /nakama/data/

```

My docker compose

```auto
version: '3'
services:
  postgres:
    container_name: postgres
    image: postgres:12.2-alpine
    environment:
      - POSTGRES_DB=nakama
      - POSTGRES_PASSWORD=localdb
    volumes:
      - data:/var/lib/postgresql/data
    expose:
      - "8080"
      - "5432"
    ports:
      - "5432:5432"
      - "8080:8080"
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres", "-d", "nakama"]
      interval: 3s
      timeout: 3s
      retries: 5
  nakama:
    container_name: nakama
    image: registry.heroiclabs.com/heroiclabs/nakama:3.17.1
    entrypoint:
      - "/bin/sh"
      - "-ecx"
      - >
          /nakama/nakama migrate up --database.address postgres:localdb@postgres:5432/nakama &&
          exec /nakama/nakama --name nakama1 --database.address postgres:localdb@postgres:5432/nakama --logger.level DEBUG --session.token_expiry_sec 7200          
    restart: always
    links:
      - "postgres:db"
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - ./:/nakama/data
    expose:
      - "7349"
      - "7350"
      - "7351"
    ports:
      - "7349:7349"
      - "7350:7350"
      - "7351:7351"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:7350/"]
      interval: 10s
      timeout: 5s
      retries: 5
volumes:
  data:

```

my tsconfig.json

```auto
{
  "files": [
    "./main.ts",
    "./match_handler.ts",
    "./health_check.ts",
  ],
  "compilerOptions": {
    "target": "es5",
    "outFile": "./build/index.js",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "typeRoots": [
      "./node_modules"
    ]
  }
}

```

my init module

```auto
function InitModule(ctx: nkruntime.Context, logger: nkruntime.Logger, nk: nkruntime.Nakama, initializer: nkruntime.Initializer) {
    logger.info("Running init module");

    initializer.registerRpc("healthcheck", healthcheck);

    createTournament(nk);
    logger.info("Created new tournament");

    logger.info("Initializing rpc for tournament reward");
    initializer.registerRpc("tournamentRewardSet", tournamentRewardSet);

    logger.info("Initializing new match module");
    initializer.registerMatch(moduleName, {
        matchInit,
        matchJoinAttempt,
        matchJoin,
        matchLeave,
        matchLoop,
        matchTerminate,
        matchSignal,
    });

    logger.info("Finished setting javascript logic");
}

```

---

<div class="post-metadata">

**Author:** ![tom](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/tom/32/676_2.png) [@tom](https://forum.heroiclabs.com/u/tom)\
**Post date:** [September 11, 2023, 8:44am UTC](https://forum.heroiclabs.com/t/goerror-failed-to-find-initmodule-function-when-using-registermatch/3129/10 "2023-09-11T08:44:20Z")

</div>

@LucaJeevanjee Please create a new topic and post the full error you are receiving. Also please include your `local.yml` file.
