How to execute a Node command in a Kotlin/JS project

Viewed 264

Kotlin/JS maintains a local Node installation, used by Yarn, etc.

Is there a way to execute commands via that installation, without using a Gradle plugin that downloads another one?

1 Answers

The task kotlinNodeJsSetup exposes the destination of the Node installation.

Using this task, it is possible to create a NodeJS task:

tasks.create<Exec>("yourTask") {
    dependsOn(":kotlinNodeJsSetup")

    val kotlinNodeJsSetup = rootProject.tasks["kotlinNodeJsSetup"] as org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsSetupTask
    workingDir = File(kotlinNodeJsSetup.destination, "bin")
    commandLine(
        "node",
        "yourScript.js",
        "arg1",
        "…",
    )
}
Related