To my knowledge, the Gradle plugin installs Node s...
# javascript
c
To my knowledge, the Gradle plugin installs Node somewhere so it can use it with Yarn & everything. Is there a way to create a custom Gradle task that uses that node installation? I only found plugins that maintain a second node installation, but that's sub-optimal. Essentially, I have a
devNpm
dependency that has a JS script I'd like to run during development. So far I've done it by using my locally installed /usr/bin/node, but that's not portable (especially for CI); I'd like to take advantage of Kotlin's Node installation.
After some research, it seems that the Node installation is stored in
~/.gradle/nodejs/node-<version>/bin/node
Is it possible to know the node version used by Gradle, to statically know what the file will be?
e
untested, just looking around the gradle plugin sources,
Copy code
NodeJsRootPlugin.apply(rootProject)
val kotlinNodeJs = the<NodeJsRootExtension>().nodeVersion
something like that might work, but how node is set up is pretty configurable and varies by platform too. might have an easier time telling Kotlin to use your existing node install
c
so this works:
Copy code
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",
    // whatever
  )
}
e
but it's not "bin" on windows
j
Indeed on Windows I don't see a
bin
subfolder there, directly node.exe:
So I believe
File(kotlinNodeJsSetup.destination, "bin")
might not be universal
c
Ah 😕 If you find out a universal way, please post in on SO and I'll mark it as accepted
j
It may be worth taking a look at how the Kotlin plugin runs it, but otherwise maybe something like
workDir = if (windows) destination else File(...)
would do the trick
e
Copy code
val kotlinNodeJs = rootProject.the<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension>()
val nodeJsEnv = kotlinNodeJs.requireConfigured()
nodeJsEnv.nodeExecutable // full path to node binary, including ".exe" on Windows, "/bin/" on UNIX