Hi everyone, I'm having trouble getting an npm jav...
# javascript
m
Hi everyone, I'm having trouble getting an npm javascript module to work with kotlin/js. In my build.gradle.kts
Copy code
implementation(npm("jwt-decode","3.1.2"))
In my kotlin code:
Copy code
@JsModule("jwt-decode")
@JsNonModule
external fun jwt_decode(token: String): dynamic
And what I see in the console output in my browser dev tools:
Copy code
GoogleAuthComponent.kt?e0b6:35 Uncaught TypeError: $module$jwt_decode is not a function
Here is the documentation of how the js library should work in vanilla javascript:
Copy code
import jwt_decode from "jwt-decode";

var token = "eyJ0eXAiO.../// jwt token";
var decoded = jwt_decode(token);
Am I missing some step? I can see the jwt-decode libary got pulled into my build node_modules directory. But it doesn't appear to be packaged up with my projects js file, and it doesn't appear to get pulled down via any other network requests from my page. Do I need to add a script tag to pull the js file down myself? And if so, is there some path at which all the node modules are available? Is there any more in-depth documentation than this? https://kotlinlang.org/docs/using-packages-from-npm.html
b
See if this helps.
Can you link to js docs where you pulled that snippet?
t
Copy code
@file:JsModule("jwt-decode")
@file:JsNonModule

@JsName("default")
external fun jwt_decode(token: String): dynamic
b
Or just this (to closer match your original snippet)
Copy code
@JsModule("jwt-decode")
@JsNonModule
@JsName("default")
external fun jwt_decode(token: String): dynamic