steambrew
steambrew

Reputation: 23

writing nodejs applications in kotlin with intellij idea ce

I am trying to develop a Nodejs application using Kotlin 1.3.11 using the IntelliJ IDEA CE development environment. Unfortunately I haven't made any progress towards a running application. To ensure everything is setup correctly I want to print out a simple "hello world".

I searched for articles or tutorials about the topic but I didn't find much about bringing those three together (Kotlin, IntelliJ, Nodejs). The most specific ones which I found are: a medium post and another post.

As far as I (believe to) know, there are three major steps:

I tried to perform the steps in different orders but I never came to a running application. Also I searched in IntelliJ's documentation but the Nodejs integration isn't a feature of the free community edition. There isn't a description how to make Kotlin and Nodejs work together too.

Has anyone here successfully tried to do that (or failed and knows why it is not going to work)? Do I have to use another IDE or to write my own build tools/toolchain?

Sincerely J.

Upvotes: 2

Views: 1614

Answers (2)

Person27
Person27

Reputation: 1

Edit: Alternatively, you could clone https://github.com/miquelbeltran/kotlin-node.js and follow the instructions in the read me.

I managed to get the Medium post to work by replacing gradle build with the following (since the post was published in 2017(!) and requires a much older version of Gradle):

Comment out the entire contents of build.gradle like so:

/*group 'node-example'
...
compileKotlin2Js.kotlinOptions {
  moduleKind = "commonjs"
  outputFile = "node/index.js"
}*/

Run this command in the command prompt: (3.4.1 was the latest version of Gradle just before the Medium post was published.)

gradle wrapper --gradle-version=3.4.1

Uncomment out build.gradle:

group 'node-example'
...
compileKotlin2Js.kotlinOptions {
  moduleKind = "commonjs"
  outputFile = "node/index.js"
}

Run this command in place of gradle build:

gradlew build

And finally run this command as in the post: (As of writing this answer on StackOverflow, Node.js does not be downgraded and the current LTS version 10.16.0 works perfectly.)

node node/index.js

Upvotes: 0

andylamax
andylamax

Reputation: 2073

I haven't done this in IDEA CE, but theoretically, this should work.

Prerequisites: You have node installed, you can execute gradle tasks

This is a Minimum Configuration, There is a comprehensive one. Add a comment if intrested for that

Step 1:
Create a new Kotlin/JS project (with gradle) and make sure that your gradle build file looks like this

group 'node-example'
version '1.0-SNAPSHOT'

buildscript {
  ext.kotlin_version = '1.3.11'
    repositories {
      mavenCentral()
    }
  dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
  }
}

apply plugin: 'kotlin2js'

repositories {
  mavenCentral()
}

dependencies {
  compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}

compileKotlin2Js.kotlinOptions {
  moduleKind = "commonjs"
  outputFile = "node/index.js"
}

task npmInit(type: Exec) {
  commandLine "npm", "init", "-y"
}

task npmInstall(type: Exec) {
  commandLine "npm", "install", "kotlin", "express", "--save"
}

task npmRun(type: Exec) {
  commandLine "node", "node/index.js"
}

npmRun.dependsOn(build)

Step 2:
After syncing your build.gradle in step 1 run the gradle tasks npmInit and npmInstall

./gradlew :npmInit
./graldew :npmInstall

Step 3:
Create your kotlin file (index.kt/main.kt/whatever.kt) in src/main/kotlin and test the code below

external fun require(module:String):dynamic

fun main(args: Array<String>) {
    println("Hello JavaScript!")

    val express = require("express")
    val app = express()

    app.get("/", { req, res ->
        res.type("text/plain")
        res.send("Kotlin/JS is kool")
    })

    app.listen(3000, {
        println("Listening on port 3000")
    })
}

Step 4: RTFA - Run The App
Run the gradle task npmRun

./gradlew :npmRun

Hope that helps

Note:
1. This template was pulled from the medium post you asked above and modified a little
2. Remember to run your gradle tasks using sudo (if you are using linux)

Upvotes: 1

Related Questions