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";
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/
I’m new to Nakama and trying to follow the TypeScript Runtime with Rollup. I have strictly followed the tutorial (except I used CockroachDB setup for docker-compose.yml), unless I made a mistake somewhere.
I had the same issue as OP, and the following steps did fix it:
rename rollup.config.js to rollup.config.mjs
remove import pkg from "./package.json"; line from rollup.config.js as it is not used anyway.
However, when I run docker compose up, I get an error:
nakama-1 | {"level":"fatal","ts":"2025-01-18T10:51:00.392Z","caller":"server/runtime_javascript.go:640","msg":"Failed to load JavaScript files","error":"SyntaxError: index.js: Line 21:1 Unexpected reserved word"}
I believe the issue is that the built JavaScript has:
export { main as default };
NakamaBackend/build/index.js
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var main$1 = {};
var hasRequiredMain;
function requireMain() {
if (hasRequiredMain) return main$1;
hasRequiredMain = 1;
function InitModule(ctx, logger, nk, initializer) {
logger.info("TypeScript module loaded.");
}
!InitModule && InitModule.bind(null);
return main$1;
}
var mainExports = requireMain();
var main = /*@__PURE__*/getDefaultExportFromCjs(mainExports);
export { main as default };
Solution: The rollup.config.mjs provided in the documentation is not correct or outdated.
Searching the forum, I came across this message:
If you do not plan on updating this documentation, then please add a disclaimer at the top, and save people the pain of unknowingly getting stuck trying to get an outdated tutorial to work.
If I replace the rollup.config.mjs contents provided by the tutorial with the version found in the GitHub repo:
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";
const extensions = [".mjs", ".js", ".ts", ".json"];
export default {
input: "./src/main.ts",
external: ["nakama-runtime"],
plugins: [
// Compile TS to check types
typescript(),
// Allows node_modules resolution
resolve(),
json(),
// Resolve CommonJS modules
commonjs(),
// Compile TS and build to ES5
babel({
extensions: extensions,
babelHelpers: "bundled",
}),
],
output: {
format: "cjs",
file: "build/index.js",
},
};
The built JavaScript is now correct and Nakama starts up correctly: