2

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:

  • calling initializing the node app via npm and using npm to install the node dependencies like kotlin and expressjs
  • creating a build.gradle to define other dependencies and tasks
  • creating an IntelliJ IDEA project

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.

4

2 回答 2

1

我在 IDEA CE 中没有这样做,但理论上,这应该可以。

先决条件:你已经安装了node,你可以执行gradle任务

这是最低配置,有综合配置。如果对此感兴趣,请添加评论

第 1 步:
创建一个新的 Kotlin/JS 项目(使用 gradle)并确保您的 gradle 构建文件如下所示

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)

第 2 步:
在第 1 步中同步后build.gradle,运行 gradle 任务npmInitnpmInstall

./gradlew :npmInit
./graldew :npmInstall

第 3 步:
其中创建您的 kotlin 文件(index.kt// main.kt)并测试以下代码whatever.ktsrc/main/kotlin

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")
    })
}

第 4 步:RT F A - 运行应用程序
运行 gradle 任务 npmRun

./gradlew :npmRun

希望有帮助

注意:
1.这个模板是从你上面问的中篇文章中提取的,并做了一些修改
2.记得使用sudo(如果你使用的是 linux)运行你的 gradle 任务

于 2019-03-02T03:36:38.797 回答
0

编辑:或者,您可以克隆https://github.com/miquelbeltran/kotlin-node.js并按照自述文件中的说明进行操作。

我设法通过替换gradle build以下内容来使 Medium 帖子正常工作(因为该帖子于 2017 年发布(!)并且需要更旧版本的 Gradle):

build.gradle像这样注释掉整个内容:

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

在命令提示符下运行此命令:(3.4.1 是发布 Medium 帖子之前的最新版本的 Gradle。)

gradle wrapper --gradle-version=3.4.1

取消注释build.gradle

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

运行此命令代替gradle build

gradlew build

最后像帖子中一样运行这个命令:(在 StackOverflow 上写这个答案时,Node.js 没有降级,当前的 LTS 版本 10.16.0 运行良好。)

node node/index.js
于 2019-06-13T16:31:06.033 回答