Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question What is the best way to include a node module into Unity WebGL?

Discussion in 'WebGL' started by tinahoeflich, May 19, 2023.

  1. tinahoeflich

    tinahoeflich

    Joined:
    May 19, 2023
    Posts:
    2
    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.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,563
    Perhaps this video helps?
    https://portal.gitnation.org/contents/extending-unity-webgl-with-javascript

    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.
     
    Last edited: May 19, 2023