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?

1 Like

You can find simple hello world project for node.js in attachment.



[hello_node.zip|attachment](upload://2z6VgyxqCrn9bW96qtm4vWvWvbM.zip) (188 KB)
1 Like

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.

https://kotlinlang.org/docs/tutorials/javascript/working-with-modules/working-with-modules.html#UsingCommonJS

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
http://techprd.com/kotlin-node-js-seed-project/
you can find the full project on my GitHub
https://github.com/techprd/kotlin_node_js_seed

2 Likes

Full of dynamic types. Do we really call that “ready”? Kotlin is nothing interesting if all variables are just dynamic.

2 Likes

Why do I need gradle in my NodeJS Kotlin project, when any other language lives just fine with npm alone?

Please excise Kotlin from the dying Java environment, make it as widespread as TypeScript is for Frontend and Backend development in NodeJs. Have you ever tried to build something like a Backend with a GraphQL API using GraphQL and Spring libs in Java/Kotlin? It is horrible, clunky, and full of undocumented and deprecated code.

Please bring Kotlin to JS without any Java-related gimmicks like gradles, JVMs, Springs, and such. Don’t let Kotlin die!

2 Likes

I think you’ll find not everyone will agree with that assessment…

6 Likes

Why do I need gradle in my NodeJS Kotlin project, when any other language lives just fine with npm alone?

You could originally create npm-only Kotlin projects (at least when using create-react-kotlin-app). I assume it was abandoned because Kotlin integrates so well with Gradle already, that it made more sense to bring better npm support to Gradle than better Kotlin support to npm. (Additionally, multiplatform would become quite a busy mess of different build tools if we ended up with the most popular build tool for each target).

3 Likes

I have been looking for a while to build my web client using kotlin, I 100% agree with this statement.

1 Like