I also seem to have another issue, this code ```va...
# javascript
a
I also seem to have another issue, this code
Copy code
val discord = require("discord.js");
val client = discord.Client()
compiles to this
Copy code
var discord = require('discord.js');
var client = discord.Client();
which gives me an error because it should instead be
Copy code
var discord = require('discord.js');
var client = new discord.Client();
How do I invoke ``new`` seeing as how its not a kotlin keyword? Edit: Im not sure if this has something to do with the way I imported the package, this is the require function, I did not make any external classes
Copy code
external fun require(module: String): dynamic
a
Basically, you need to bind the require-able module to an external object/interface/class, for example:
Copy code
@JsModule("discord.js")
external object Discord {
   class Client {
   }
}
And then the compiler will translate access to the object
Discord
into
require("discord.js")
(effectively)
and then
Discord.Client()
should translate into a
new
operation
you may need to specify the kotlin/js flag for the module kind (should be ‘commonjs’ for node)
a
Thanks for the quick reply! Should I provide any further implementation in the external object or should that suffice? I still seem to be getting the same error
a
no, just that annotation should be enough. you’re getting the error because it’s not calling the constructor correctly? Can you find the call in the generated JS?
(in case it wasn’t clear, you shouldn’t need to call
require
from Kotlin unless you have an explicit need for a dynamic require, or delaying the require of the module until that moment)
For me, this Kotlin code:
Copy code
@JsModule("discord.js")
external object Discord {
    class Client { }
}

fun createDiscordClient() = Discord.Client()
got translated to:
Copy code
var Discord$Client = $module$discord_js.Client;
  function createDiscordClient() {
    return new Discord$Client();
  }
which looks to me like what you need
a
Hey sorry for the late reply, this seems to work (does not give errors yet at least), I basically did what you mentioned earlier but added the ``: Discord`` part for the class thanks!
a
The
val discord
property isn’t being used, as
Discord
is an object: you can just remove that line
❤️ 1
a
that is a good observation thanks
❤️ 1