Require is not defined at index.js

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.

Thank you in advance for your help!

Hello @amirnazarpour,

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.

You can see an example on how to setup your project with rollup here.

Beware that Node.js modules may not be compatible as well as it is not a Node environment.

Hope this helps.

1 Like

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!

Happy to report that I found a workaround (for anyone else who runs into this particular issue).

The library “jsrsasign” can be installed in the Nakama project and doesn’t cause any issues.

Once installed you can use the JWT features of jsrasign as documented here:

Main thing to watch out for is that you need to use the Nakama provided base64 and binary string code when doing the decoding. For example:

let payloadJSON = nk.binaryToString(nk.base64Decode(payloadB64))

I appreciate that you share this info with us
thanks again