Node.js proper module export

Hi,

I’m currently updating my multiplatform Hello Kotlin project
https://github.com/wojta/hello-kotlin/tree/kotlin-1.2-beta with new multiplatform support Kotlin introduced. There are not many problems related to that.

But there is another problem not related to multiplatform support. I have project module called hello_js_node_app and it’s supposed to be run as Node.js app. It has one dependency on other kotlin2Js module called hello_lib_js. I need to export properly .js files from jars of both modules to have it imported by require() module.
So far I have this build.gradle and assembleJs task expands everything to the node_modules.

node_modules look like this: https://drive.google.com/file/d/0B-k1pGbOW-o3VmNiVXZwNEZCVXc/preview

Problem is that when I execute node ./app.js, it can’t find kotlin module dependecy. I’m not sure about Node module directory structure, but I have not deep knowledge of Node.

node error message
vsazel@vsazel-thinkpad ~/git/ghdrive/experiments/hello-kotlin/hello_js_node_app $ node ./app.js 
/home/vsazel/git/ghdrive/experiments/hello-kotlin/hello_js_node_app/node_modules/hello_lib_js.js:2
  throw new Error("Error loading module 'hello_lib_js'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'hello_lib_js'.");
  ^

Error: Error loading module 'hello_lib_js'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'hello_lib_js'.
    at Object.<anonymous> (/home/vsazel/git/ghdrive/experiments/hello-kotlin/hello_js_node_app/node_modules/hello_lib_js.js:2:9)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/home/vsazel/git/ghdrive/experiments/hello-kotlin/hello_js_node_app/node_modules/hello_js_node_app.js:24:38)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

So is there some more convinent or correct way to export Kotlin modules as node modules?

Thanks

Looks like for some reason moduleKind property did not apply properly. Did you set up moduleKind=commonjs for hello_lib build script? Can you provide more information? Perhaps, share your project?

Ah, I just realized that you provided link to whole project. Yes, I could not find moduleKind in hello_lib_js. Please add it the way similar you did in hello_js_node_app.

1 Like

Thanks for pointing me to the right direction. I managed to get node modules working. I need only now to get it working with the client-side JS again. Maybe it will help using browserify or something like that.

Yes, you can use browserify or webpack for this purpose. Another way is to use “umd” as a module kind, which is supported by both browsers and node.