How can set the scope for the output npm package (i.e. set the package name)?

I have a fairly standard Kotlin Multiplatform library project that produces library targets for android, jvm, and JavaScript. My project includes a single module, let’s call it ‘my-module’. When I assemble the project, I wind up with a publish-able npm package in the build/js/packages/my-module directory. The produced package.json looks like this:
{
“main”: “kotlin/my-module.js”,
“devDependencies”: {},
“dependencies”: {
“webpack”: “4.29.6”,
“webpack-cli”: “3.3.0”,
“source-map-loader”: “0.2.4”,
“source-map-support”: “0.5.12”,
“webpack-dev-server”: “3.3.1”,
“kotlin”: “1.3.41”
},
“name”: “my-module”,
“version”: “1.0.20”
}

I want to publish this package to my organization’s npm registry. However, the registry requires that the package name in the package.json be scoped to the organization. So, in the package.json, the name should be:
“name”: “@my-organization/my-module”

Is there some way I can achieve this with a setting in my gradle configuration so that at compile-time the package.json gets produced with the correct scoped name?

1 Like

I’m facing the same issue in my multi-platform project.
I made some custom plugins to alter the generated package.json to include my organization name plus some other info.
However I noticed this is not sufficient in the case one is making a multi-project build were some js-project must be a dependency for some other js-project.
This happens because the many require("other-js-project") generated by the kotlin compiler in the js sources will still reference any other project without taking care of specifying the organization.

I believe the kotlin plugin for gradle should be extended to support this feature.

However, if you only need to inject an organization name into a single kt-js project, my solution should be fine:

Is there any solution here please? I’m facing the same issue
I always have to use

let package= require("packageName").com.company.package.ClassName

I would like to access directly to the class eg:

let package= require("packageName").ClassName