gradle-node-plugin icon indicating copy to clipboard operation
gradle-node-plugin copied to clipboard

Support using node from other project

Open daosavaj opened this issue 5 years ago • 10 comments

Original question So i have a multi module project with two subprojects that have Node dependencies. What is the best way to have Node only download once.

/subproject1 ...build.gradle /subproject2 ...build.gradle build.gradle

daosavaj avatar Jan 20 '20 14:01 daosavaj

I guess what you really want is just one instance of node/npm, to which I unfortunately don't have a good answer. But with gradle's caching and such it should only download node once, even though it unpacks twice.

deepy avatar Jan 20 '20 14:01 deepy

Ah okay, Thanks for the quick reply!

daosavaj avatar Jan 20 '20 14:01 daosavaj

This is a feature we probably want to support in the future though, so I'm editing the title and re-opening this issue :-) Although for multiple reasons it's going to be tough to implement, but at worst we should be able to provide a hacky way to do it.

deepy avatar Jan 20 '20 14:01 deepy

I am able to make this work to an extent. All you really have to do is set the workDir and npmWorkDir to the same directory like this:

workDir = file("${rootDir}/nodejs") npmWorkDir = file("${rootDir}/npm") download = true

This fails if you make any attempt to run the build in parallel, however, because it still wants to unpack npm and nodejs every time. I was hoping it would detect that node was already installed and not try to do it a second time, but that is not the case. What I found is that if download=true, it always unpacks it. If download=false, it uses your system copy.

myronu avatar Feb 21 '20 15:02 myronu

I guess the easiest way forward would be to add an option to download = false to specify where node is, that'd let you work around this one by having one download = true and multiple = false pointing to that

deepy avatar Feb 21 '20 18:02 deepy

Can't you just always install in the rootProject's .gradle directory instead of the sub-project's .gradle directory?

eekboom avatar Apr 09 '20 13:04 eekboom

This idea may be a good solution, but it would be necessary to ensure that there can be no conflict in case of multiple projects of the same multi-project build using different Node.js configurations (for instance different version). Or maybe we should force all projects to use the same Node.js configuration?

bsautel avatar Apr 29 '20 15:04 bsautel

@bsautel Just add distinct installation directories per version ?

loganmzz avatar Mar 24 '23 11:03 loganmzz

At least with version 3.5 (not sure about the newer ones), it seems that when node { .... download = false .... } is used in a project, the paths to the node/npm commands are not resolved correctly (according the specified version & current platform), so NpmTask and NodeTask end-up calling just "npm" or "node" respectively, and either fail (no such command) or use binaries from PATH (not what you want)

However, it seem that setting up node in all the projects the sameway (with download = true and setting workDir / npmWorkDir to the same fixed location ) and then setting the tasks named nodeSetup npmSetup as disabled (e.g. enabled = false) in all projects but one will do the trick - the node/npm paths are correctly resolved everywhere, and the actuall downloading/unpacking will happen just once (in the non-disabled tasks)

Note: you can alternatively locate tasks of type com.github.gradle.node.task.NodeSetupTask / com.github.gradle.node.npm.task.NpmSetupTask instead of going by name

michkot avatar Apr 16 '24 23:04 michkot

There's two specific ways of working that this plugin supports, one is "always use this specific version of node" (download = true) and the other is "Use my own installation of node" (download = false)

Disabling nodeSetup and npmSetup is a nifty way of doing this, I suspect thanks to inputs/outputs it should work fine. And thinking about it, maybe it's fine to wire things up that way, but I have to look into this a bit closer

But as long as you use the same version of node everywhere thanks to Gradle's dependency mechanism you should only be downloading nodejs once (and if you use a blank npm version you download npm 0 times :tada:) so you'll be saving space from unpacking node but still have the same amount of downloads

deepy avatar Apr 17 '24 15:04 deepy