Sunil Kumar
06/11/2024, 4:27 AMtype Nullable<T> = T | null | undefined
export declare class Greeting {
constructor();
greet(): string;
}
export declare class JSPlatform /* implements Platform */ {
constructor();
}
export as namespace KMPProjectWithoutSharedUi_shared;
But here while im using Greeting class, its showing undefined in console. Any idea how to use the exported npm package?Artem Kobzar
06/11/2024, 4:35 AMjs
block in your build.gradle.kts?Sunil Kumar
06/11/2024, 4:40 AMjs(IR) {
moduleName = "kmm-shared-library"
version = kmmVersion
nodejs() //for publishing library as npm package to npm
binaries.library() //for publishing library as npm package to npm, executable is for running in browser
generateTypeScriptDefinitions()
binaries.withType<JsIrBinary>().all {
this.linkTask.configure {
kotlinOptions {
sourceMap = false
}
}
}
}
Im using it like below:
import { Greeting } from "shared";
....
console.log("kmmSharedLibrary=", Greeting());
ANd this is the generated package.json file in shared/build/packages/js
{
"main": "kmm-shared-library.js",
"types": "kmm-shared-library.d.ts",
"version": "0.0.1",
"name": "shared",
"dependencies": {
"node-fetch": "2.6.7",
"abort-controller": "3.0.0",
"ws": "8.5.0",
"format-util": "^1.0.5"
}
}
Edoardo Luppi
06/11/2024, 8:05 AMArtem Kobzar
06/11/2024, 9:00 AMuseEsModules
into the js section?Edoardo Luppi
06/11/2024, 9:01 AMSunil Kumar
06/11/2024, 9:46 AMimport {
protoOf180f3jzyo7rfj as protoOf,
classMetawt99a3kyl3us as classMeta,
setMetadataForzkg9su7xd76l as setMetadataFor,
VOID7hggqo3abtya as VOID,
} from './kotlin-kotlin-stdlib.mjs';
//region block: imports
//endregion
//region block: pre-declaration
setMetadataFor(Greeting, 'Greeting', classMeta, VOID, VOID, Greeting);
setMetadataFor(JSPlatform, 'JSPlatform', classMeta, VOID, VOID, JSPlatform);
//endregion
function Greeting() {
}
protoOf(Greeting).greet = function () {
return 'Hello,!';
};
function JSPlatform() {
this.a_1 = 'Kotlin/Js';
}
protoOf(JSPlatform).b = function () {
return this.a_1;
};
//region block: exports
export {
Greeting as Greeting,
JSPlatform as JSPlatform,
};
//endregion
they are exported as well i guess.Sunil Kumar
06/11/2024, 9:48 AMArtem Kobzar
06/11/2024, 11:03 AMimport * as lib from "shared"
console.log(lib)
Sunil Kumar
06/11/2024, 11:42 AMconsole.log("lib=", lib);
console.log("Greeting=", lib.Greeting);
console.log("Greeting()=", lib.Greeting());
And here are the console logs:Sunil Kumar
06/11/2024, 11:43 AMArtem Kobzar
06/11/2024, 11:53 AMnew
keyword.
So, you just used the constructor in a wrong way:
const instance = new Greeting()
Edoardo Luppi
06/11/2024, 11:54 AMArtem Kobzar
06/11/2024, 11:54 AMundefined
Artem Kobzar
06/11/2024, 11:54 AMArtem Kobzar
06/11/2024, 11:57 AMjs
block (if you use 2.0 Kotlin):
js {
compilerOptions {
target = "es2015"
}
}
Artem Kobzar
06/11/2024, 11:57 AMSunil Kumar
06/11/2024, 12:02 PM