I have to include a node module into my Unity WebGL Project.
Since require('module'); is not possible in .jslib files, (it will throw a ReferenceError: require is not defined) what is the best way to stil include a node module?
I have been researching for days now and can’t seem to find a proper way to do it, so I would really appreciate if someone can help me on this.
I have no exp with NodeJs but since NodeJS is a server technology you cannot embed it into your Unity app or its JS wrapper. Unity WebGL runs in the browser, it’s basically a client/remote service/program that somehow needs to communicate with the NodeJS server interface.
I would first try to approach it the common way web services “talk” to each other, via POST/GET using Unity’s WebRequest API.
You can also make calls to Javascript using the C# interface. Here’s more info but it may be outdated. Calling your own javascript from C# means you can then route calls to the NodeJS lib and back to Unity, but I would advise that only if you are more comfortable within the JS world or if it’s technically required to have that JS interface for some reason.
Browsers do not have require(), so it is not possible to run any code that uses it in a browser as-is. This limitation does not have to do with .jslib files, which are Emscripten JS library files.
Alternative module import mechanisms to Node.jses require() are ES6 Modules, or different web frameworks. These are discussed e.g. in this StackOverflow thread.
These would be something that the Node.js package author would need to have thought about while developing their package. I.e. they would have made their package dual-usable in Node.js via require() and then maybe as ES modules using the browser-provided ES import syntax.