1

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 Answer 1

2

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",
        "…",
    )
}
Sign up to request clarification or add additional context in comments.

3 Comments

As mentioned in the Slack thread, there is no bin subfolder on Windows for the executable (node.exe is directly there). You might need to define the work dir differently based on OS, unless there is a better way
It's possible to leverage Gradle's Task Configuration Avoidance by using val yourTask by registering(Exec::class) { for the declaration. Also, had to use "./node" instead of "node".
Configuration already finalized for previous property values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.