hi, do you know if there is some article/post/plug...
# javascript
e
hi, do you know if there is some article/post/plugin for publishing a kotlin/js library to npm? I was using npm-publish but it seems like it uses the module name(in my case is something like
SomeLibrary
) instead of the base name from
js(IR) { moduleName = "some-library" ... }
and when I want to publish to npm I’m getting this error
npm ERR! 400 Bad Request - PUT <https://registry.npmjs.org/SomeLibrary> - "SomeLibrary" is invalid for new packages
where I would expect to publish it with the
moduleName
I defined form the js gradle block. A “workaround” was to rename my module to
SomeLibrary
to
some-library
but since I’m also targeting iOS I need to have it like
SomeLibrary
since iOS doesn’t support names like
some-library
. Thank you in advance!
Looks into various DSL options under
npmPublish
task
you should be able to change module/package name via that route
e
thank you for the quick response, I’m trying the following where
SomeLibrary
is my modules’ name
Copy code
npmPublish {
    packages {
        register("SomeLibrary") {
            packageName.set("some-library")
        }
    }
...
}
but I’m still facing the same issue
Copy code
Request - PUT <https://registry.npmjs.org/SomeLibrary> - "SomeLibrary" is invalid for new packages
do you know if am I missing something?
s
Did you try this ?
Copy code
named("SomeLibrary") {
    packageJson {
        name.set("some-library")
    }
}
e
I just tried this and it worked like a charm! 🙌
Copy code
npmPublish {
    packages["js"].packageName.set("some-library")
...
}
thank you so much for the help! 🙏
🎉 1
b
Although it's better to use @shaktiman_droid suggestion but replace register with named. That way you don't force package resolution eagerly
👍🏽 1
Register doesn't work here, because the plugin already autoconfigures a package for each js target. So you only want to retrieve and lazily modify it, which is exactly what named does
And finally, just so we're all clear, npmPublish {} is an extension, not a task (I always hated that groovy would resolve both at the same level) 🤠
👍🏽 1
e
you mean like this?
Copy code
packages {
    register("SomeLibrary")
    named("SomeLibrary") {
        packageJson {
            packageName.set("some-library")
        }
    }
}
it gives me the same error :c
Copy code
Request - PUT <https://registry.npmjs.org/SomeLibrary> - "SomeLibrary" is invalid for new packages
I also tried
Copy code
packages {
    named("js") {
        packageJson {
            packageName.set("some-library")
        }
    }
}
but gives me this error
Copy code
> The value for property 'packageName' is final and cannot be changed any further.
b
Second opinion, but don't go in packageJson block
The fact that it breaks when done inside packageJson block is a bug - I'll look into it.
e
this worked too! thank you
Copy code
packages {
    named("js") {
        packageName.set("cpf-validator")
    }
}
could you please elaborate on the differences with
Copy code
packages["js"].packageName.set("some-library")
? I didn’t get it well and I would like to understand why one option is better that the another one
b
Well entire api is wired lazily and only resolved when needed. However when you (as opposed to a task) access any of it directly, you force to resolve it prematurely, which can cause some race conditions and make it unable to receive newer values from kotlin js plugin or any other lazy sources
That's why you need to call .set() on properties as opposed to good old fashioned =
Google around "gradle provider api" if you want to learn more
e
oh I see! thanks for the help and the explanation! 🙌
b
🙏 1