I am currently developing a custom module for Nakama server, and I’ve run into a JavaScript runtime error that I’m struggling to resolve. I am trying to use the jsonwebtoken library within my module, but I keep encountering a reference error related to the use of CommonJS syntax.
Here is the specific error message I’m seeing in my Nakama server logs:
ReferenceError: require is not defined at index.js
And this is the line of code causing the issue:
var jwt = require('jsonwebtoken');
I understand that this might be related to Nakama’s JavaScript environment not supporting the CommonJS require syntax. I tried using ES module syntax with import jwt from 'jsonwebtoken'; , but I encountered different issues.
Could anyone advise on how to properly integrate third-party libraries such as jsonwebtoken within Nakama’s JavaScript runtime? Any suggestions on configuring the environment or examples of how to import modules correctly in this context would be greatly appreciated.
Nakama only supports ES5 JS, it cannot resolve modules, you’ll need to use something like Rollup to transpile and resolve any imports into a single bundled file that JS can load.
I am actually having the exact same problem. For our project we need to support signing JWT (JSON Web Tokens) on the server from our typescript code, sending them to the client, getting them back, and then verifying the JWT. This would be trivial with the jsonwebtoken npm library, but since that apparently won’t work we need suggestions on how to sign/verify the JWTs.
Are there standalone javascript/typescript implementations that we could use that are compatible with Nakama? Or, alternatively, is there any way to get access to the go libraries that handle JWTs on the go side (from our typescript code)?
This is a critical feature and feels like something that Nakama should provide out of the box, especially since JWTs are used under the hood for Nakama.
Also… it would be really nice if Nakama had better support for including external code libraries, especially if it could support node. We keep running into this problem over and over again and it is seriously hurting our game development process. You would be our heroes!