Error during installing Nakama Typescript Server Runtime

I had to write some hooks for our project. That’s why I am trying to setup Nakama typescript server runtime using TypeScript Runtime tutorial. But when I am using the command “npm run build” I am having the below error.

(node:60942) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
[!] RollupError: Node tried to load your configuration file as CommonJS even though it is likely an ES module. To resolve this, change the extension of your configuration to ".mjs", set "type": "module" in your package.json file or pass the "--bundleConfigAsCjs" flag.

Original error: Cannot use import statement outside a module
https://rollupjs.org/guide/en/#--bundleconfigascjs
/home/ashik/ts-project/rollup.config.js:1
import resolve from "@rollup/plugin-node-resolve";

Hello @Shahariar_Ashik,

Could you post the contents of the configuration files mentioned in this section of the documentation?

package.json

{
  "name": "ts-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "rollup -c",
    "type-check": "tsc --noEmit"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.19.6",
    "@babel/plugin-external-helpers": "^7.18.6",
    "@babel/preset-env": "^7.19.4",
    "@rollup/plugin-babel": "^6.0.2",
    "@rollup/plugin-commonjs": "^23.0.2",
    "@rollup/plugin-json": "^5.0.1",
    "@rollup/plugin-node-resolve": "^15.0.1",
    "@rollup/plugin-typescript": "^9.0.2",
    "rollup": "^3.2.3",
    "tslib": "^2.4.0",
    "typescript": "^4.8.4"
  },
  "dependencies": {
    "nakama-runtime": "github:heroiclabs/nakama-common"
  }
}

rollup.config.js

import resolve from "@rollup/plugin-node-resolve";
import commonJS from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import babel from "@rollup/plugin-babel";
import typescript from "@rollup/plugin-typescript";
import pkg from "./package.json";

const extensions = [".mjs", ".js", ".ts", ".json"];

export default {
  input: "./src/main.ts",
  external: ["nakama-runtime"],
  plugins: [
    // Allows node_modules resolution
    resolve({ extensions }),

    // Compile TypeScript
    typescript(),

    json(),

    // Resolve CommonJS modules
    commonJS({ extensions }),

    // Transpile to ES5
    babel({
      extensions,
      babelHelpers: "bundled",
    }),
  ],
  output: {
    file: "build/index.js",
  },
};

babel.config.json

{
  "presets": ["@babel/env"],
  "plugins": []
}

tsconfig.json

{
  "compilerOptions": {
    "noImplicitReturns": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "noUnusedLocals": true,
    "removeComments": true,
    "target": "es5",
    "module": "ESNext",
    "strict": false
  },
  "files": ["./node_modules/nakama-runtime/index.d.ts"],
  "include": ["src/**/*"],
  "exclude": ["node_modules", "build"]
}

main.ts

function InitModule(
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  initializer: nkruntime.Initializer
) {
  logger.info("TypeScript module loaded.");
}

// Reference InitModule to avoid it getting removed on build
!InitModule && InitModule.bind(null);

local.yml

console:
    max_message_size_bytes: 409600
  logger:
    level: "DEBUG"
  runtime:
    js_entrypoint: "build/index.js"
  session:
    token_expiry_sec: 7200 # 2 hours
  socket:
    max_message_size_bytes: 4096 # reserved buffer
    max_request_size_bytes: 131072

Dockerfile

FROM node:alpine AS node-builder

WORKDIR /backend

COPY package*.json .
RUN npm install

COPY . .
RUN npm run build

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

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

docker-compose.yml

version: "3"
services:
  postgres:
    command: postgres -c shared_preload_libraries=pg_stat_statements -c pg_stat_statements.track=all
    environment:
      - POSTGRES_DB=nakama
      - POSTGRES_PASSWORD=localdb
    expose:
      - "8080"
      - "5432"
    image: postgres:12.2-alpine
    ports:
      - "5432:5432"
      - "8080:8080"
    volumes:
      - data:/var/lib/postgresql/data

  nakama:
    build: .
    depends_on:
      - postgres
    entrypoint:
      - "/bin/sh"
      - "-ecx"
      - >
        /nakama/nakama migrate up --database.address postgres:localdb@postgres:5432/nakama &&
        exec /nakama/nakama --config /nakama/data/local.yml --database.address postgres:localdb@postgres:5432/nakama
    expose:
      - "7349"
      - "7350"
      - "7351"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:7350/"]
      interval: 10s
      timeout: 5s
      retries: 5
    links:
      - "postgres:db"
    ports:
      - "7349:7349"
      - "7350:7350"
      - "7351:7351"
    restart: unless-stopped

volumes:
  data:

Does problem go away if you rename rollup.config.js to the rollup.config.mjs or add "type": "module" to your package.json as error suggests?

If I convert rollup.config.js to rollup.config.mjs it now gives below error:

[!] TypeError: Unknown file extension ".json" for /home/ashik/ts-project/package.json

OK, I’ll try to reproduce problem using files you provided and let you know.

1 Like

okay thanks a lot.

OK, I made it work, with these changes:

  1. rename rollup.config.js to rollup.config.mjs
  2. remove import pkg from "./package.json"; line from rollup.config.js as it is not used anyway.

Let me know if it fixes for you too, once confirmed we’ll update docs.

npm run build didn’t give any error this time. But after running “sudo docker compose up” it shows like below:

Finally it worked. But now I am having one more issue. I can’t register rpc functions.
main.ts

let InitModule: nkruntime.InitModule = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  initializer: nkruntime.Initializer
) {
  initializer.registerRpc("healthcheck", rpcHealthcheck);
  logger.info("My Custom Module initialized.");
};

// Reference InitModule to avoid it getting removed on build
!InitModule && InitModule.bind(null);

healthcheck.ts

function rpcHealthcheck(
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  payload: string
) {
  logger.info("healthcheck rpc called");
  return JSON.stringify({ succeed: true });
}

tsconfig.json

{
  "compilerOptions": {
    "noImplicitReturns": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "noUnusedLocals": true,
    "removeComments": true,
    "target": "es5",
    "module": "ESNext",
    "strict": false
  },
  "files": ["./node_modules/nakama-runtime/index.d.ts", "./healthcheck.ts"],
  "include": ["src/**/*"],
  "exclude": ["node_modules", "build"]
}