How is Kotlin intended to be used with Node.js?


The Javascript output of the Kotlin compiler - the way I have it set up, anyway - is one file for Kotlin's standard library and one for the compiled form of all of my Kotlin files. The file with the standard library in it starts out by creating a variable `Kotlin` in the global scope and then it initializes it and assigns it to module.exports so it is available both when included in a web page with the `script` tag and when using the `require()` function in Node. The compiled form of my code, however, is just an immediately invoked function expression that expects `Kotlin` to already be in the global scope. Node is specifically designed so that separate files don't pollute the global scope, though, so there is no obvious way to make this file work with Node. The best I can think of is to create my own build script to invoke the compiler and then modify its output, which would partially defeat the purpose of using such an expensive IDE.

I need solutions that don’t involve rewriting my entire project in Kotlin - though I’m considering doing this eventually, it’s not a realistic proposition right now because that would involve troubleshooting an entire project all at once while having basically no automated tests passing for much of the time it takes to do this. I also won’t simply use the JVM as my target because that requires a complete rewrite and I’d have to find or write replacements for all of the third party libraries I’m using.


In settings, in Build, Execution and Deployment section, under Compiler / Kotlin Compiler, there is output file prefix and postfix settings. These are paths to files with text that will be prepended and appended to compiler's output. Does it help in your particular case?


You can find simple hello world project for node.js in attachment. (188 KB)


So far for this question and I just know and start learning Kotlin.
Anyway I’ve just read a docs that fully explain this questions.


I think at this point Kotlin Js has been evolved much and it is ready for Node.js application.
you can now even write your node.js app entirely with Kotlin.
as a proof of concept I created Node.js Express.js project using Kotlin
you can find the full project on my GitHub