# Require is not defined at index.js

**URL:** <https://forum.heroiclabs.com/t/require-is-not-defined-at-index-js/5184>\
**Category:** Runtime Framework\
**Tags:** typescript\
**Created:** [April 20, 2024, 10:48am UTC](https://forum.heroiclabs.com/t/require-is-not-defined-at-index-js/5184 "2024-04-20T10:48:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amirnazarpour](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/amirnazarpour/32/2131_2.png) [@amirnazarpour](https://forum.heroiclabs.com/u/amirnazarpour)\
**Post date:** [April 20, 2024, 10:48am UTC](https://forum.heroiclabs.com/t/require-is-not-defined-at-index-js/5184/1 "2024-04-20T10:48:40Z")

</div>

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:

```auto
ReferenceError: require is not defined at index.js

```

And this is the line of code causing the issue:

```auto
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!

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [April 22, 2024, 10:31am UTC](https://forum.heroiclabs.com/t/require-is-not-defined-at-index-js/5184/2 "2024-04-22T10:31:33Z")

</div>

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](https://github.com/heroiclabs/nakama-project-template/tree/spe/rollup).

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

Hope this helps.

---

<div class="post-metadata">

**Author:** ![colin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@colin](https://forum.heroiclabs.com/u/colin)\
**Post date:** [April 26, 2024, 10:11pm UTC](https://forum.heroiclabs.com/t/require-is-not-defined-at-index-js/5184/3 "2024-04-26T22:11:34Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![colin](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@colin](https://forum.heroiclabs.com/u/colin)\
**Post date:** [April 26, 2024, 11:31pm UTC](https://forum.heroiclabs.com/t/require-is-not-defined-at-index-js/5184/4 "2024-04-26T23:31:23Z")

</div>

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.

> **[GitHub - kjur/jsrsasign: The 'jsrsasign' (RSA-Sign JavaScript Library) is an...](https://github.com/kjur/jsrsasign)**
>
> The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free cryptography library supporting RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 cert...

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

> **[Tutorial for JWT generation](https://github.com/kjur/jsrsasign/wiki/Tutorial-for-JWT-generation)**
>
> The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free cryptography library supporting RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 cert...

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))

---

<div class="post-metadata">

**Author:** ![amirnazarpour](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/amirnazarpour/32/2131_2.png) [@amirnazarpour](https://forum.heroiclabs.com/u/amirnazarpour)\
**Post date:** [May 5, 2024, 6:23pm UTC](https://forum.heroiclabs.com/t/require-is-not-defined-at-index-js/5184/5 "2024-05-05T18:23:23Z")

</div>

I appreciate that you share this info with us  
thanks again

---

<div class="post-metadata">

**Author:** ![AlexandreK38](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/alexandrek38/32/1166_2.png) [@AlexandreK38](https://forum.heroiclabs.com/u/AlexandreK38)\
**Post date:** [July 4, 2025, 12:08pm UTC](https://forum.heroiclabs.com/t/require-is-not-defined-at-index-js/5184/6 "2025-07-04T12:08:45Z")

</div>

wow! I’ve been dealing with the `jsonwebtoken` issue too and also look for `jose` but it’s not compatible with `es5`; you saved my day, many thanks!
