This page contains a collection of frequently asked questions.
See this Kotlin DSL example. It shows how to use all the configuration properties.
Just add to your bundle task filesets (in and out) which this task depends on:
task bundle(type: YarnTask) {
inputs.files(fileTree('node_modules'))
inputs.files(fileTree('src'))
inputs.file('package.json')
inputs.file('webpack.config.js')
outputs.dir('build/resources/static')
dependsOn yarn_install
args = ['run', 'build']
}More info in Gradle doc
node {
npmInstallCommand = System.getenv("CI") ? 'ci' : 'install'
}This can be done adding some arguments to the already defined npmInstall-task. To set the log level to silly do this:
npmInstall.args = ['--loglevel', 'silly']This can be done by adding to the arguments for the already defined npmSetup task.
tasks.npmSetup {
doFirst {
args = args + ['--registry', 'http://myregistry.npm.com']
}
}You can also add any other arguments to this list that work with npm install i.e. more verbose modes.
Some packages (for instance Grunt, Gulp, Angular CLI, mocha...) provide some command line tools.
Instead of installing them globally, install them as devDependencies and run them using npx through the NpxTask.
Read more regarding the NpxTask here.
Here are some examples:
Install the grunt-cli package:
npm install --save-dev grunt-cliFor a build described in Gruntfile.js with sources in the src directory and output in the dist directory:
task buildWebapp(type: NpxTask) {
dependsOn npmInstall
npxCommand = "grunt"
args = ["build"]
inputs.file("Gruntfile.js")
inputs.dir("src")
inputs.dir("node_modules")
outputs.dir("dist")
}The task will only run if needed.
Install the gulp-cli package:
npm install gulp-cliFor a build described in gulpfile.js with sources in the src directory and output in the dist directory:
task buildWebapp(type: NpxTask) {
dependsOn npmInstall
npxCommand = "gulp"
args = ["build"]
inputs.file("gulpfile.js")
inputs.dir("src")
inputs.dir("node_modules")
outputs.dir("dist")
}The task will only run if needed.
NodeTask, NpmTask, NpxTask and YarnTask are some wrappers around the core Exec task.
They have several parameters that enable to customize the way the corresponding command is launched.
The ignoreExitValue property enables to avoid the task failing if the process exit value is not 0:
task myScript(type: NodeTask) {
script = file('src/scripts/my.js')
ignoreExitValue = true
}The workingDirectory option enables to change the working directory of the process. Note that some commands such as npm
force the working directory to be the one in which the package.json file is located.
This option is most of the time useless.
task myScript(type: NodeTask) {
script = file('src/file.js')
workingDir = file('./customWorkingDirectory')
}The environment option enables to define some new environment variables or override some existing ones:
task command(type: NpxTask) {
command = 'aCommand'
environment = ['CUSTOM_VARIABLE': 'Hello world']
}The execOverrides option enables to customize all the other thinks that can be configured in an ExecSpec thanks to
a closure that takes the ExecSpec as parameter. Note that it is executed last, possibly overriding already set
parameters such as the working directory.
task myScript(type: NpmTask) {
npmCommand = ['run', 'hello']
execOverrides {
// The it variable contains the `ExecSpec`
it.ignoreExitValue = true
// We can also omit it since "it" is implicit
workingDir = file('./myWorkingDirectory')
standardOutput = new FileOutputStream('logs/my.log')
}
}If your network requires using a proxy to access to the internet, you probably already
configured Gradle to use the proxy.
In this case, the plugin will automatically apply the proxy configuration to all npm and yarn commands.
Note that:
- This does not work with
npxsince it does not support proxy usage - This does work either for all
nodecommands. It's thenodescript's responsibility to use the proxy or not - For
npmandyarn, it will only work for network requests done directly by the tool (for instance downloading a dependency). This will not work if you run a Node.js script for instance vianpm run.
To disable this behavior, set useGradleProxySettings to false in the node extension. In this case, the plugin will
do nothing regarding proxy and you may want to configure it manually, for instance using the .npmrc file as
explained here for npm.
How do I ignore some files of the node_modules directory that are modified by the build and prevent tasks from being up-to-date ?
NpmInstallTask and YarnInstallTask have an option that enables to exclude some files from the task's output.
Its type is a closure that contains a FileTree
whose root directory is node_modules.
With npm:
npmInstall {
nodeModulesOutputFilter {
exclude("package/package.json")
}
}Note that the exclude method comes from a FileTree.
It can be also written this way:
nodeModulesOutputFilter {
fileTree -> fileTree.exclude("package/package.json")
}With yarn:
yarn {
nodeModulesOutputFilter {
exclude("package/**")
exclude("anotherPackage")
}
}Note: the Gradle's up-to-date checking is much slower when using this option. See issue #63.