Configure global Node defaults

The Node.js distribution can be supplied in three possible ways:

build.gradle
nodejs {
    executableByVersion('7.10.0') (1)
}

nodejs {
    executableByPath('/path/to/node') (2)
}

nodejs {
    executableBySearchPath('node') (3)
}
1 Supply a version and Gradle will take care of downloading the distribution and caching it. It will then use the cached version for running node. This is the preferred way of running Node.js with Gradle as it offers better control over reproducible builds.
2 Supply a path to the node executable. This works for people that have Node.js installed in standard locations.
3 Tell Gradle to look for node (or node.exe) in the system search path.

If no executable is configured, then Gradle will attempt to download the version specified in NodeJSExtension.NODEJS_DEFAULT. If Gradle runs on a non-supported platform this will fail.

Using nodeexec

It is possible to run node directly from Gradle using the nodeexec project execution extension. It operates in a similar fashion to Gradle’s Exec task, but it has its own node nuances, which the user needs to set up correctly.

result = nodejs.exec(FORWARD_AND_CAPTURE, FORWARD) {
    script {
        path = 'node_modules/gulp/bin/gulp.js' (1)
        args '--help' (2)
    }
    entrypoint {
        workingDir(npm.homeDirectory) (3)
        executable = nodejs.executable.get() (4)
    }
}
1 Specify the name of the script. It can be absolute of relative to the working directory.
2 Specify any arguments that will be passed to the script.
3 Set the working directory for the execution. If this is something installed via NPM, then using the location of the NPM homedirectory from the npm extension will simplify life.
4 Specify the location of the node executable. There are various ways of doing this, but an easy way is to ask the node extension to resolve it.

Consult the NodeJSExecSpec API documentation for full details of all settings.