Gradle and npm are build tool giants of their respective Java and JavaScript worlds. But differences in how each tool builds software make it tough to switch from one to the other. To help you bridge the gap, this article explains the main differences and similarities, giving you a deeper understanding of these powerful build tools.

What is Gradle?

Gradle is a build automation tool, mainly used to build Java applications. It runs on the Java Virtual Machine (JVM) and automates the process of compiling Java source code into bytecode. To enable Java code reuse, Gradle handles fetching 3rd party libraries, known as dependencies, from central repositories such as Maven Central.

  • first released in 2008

  • used by 38% of Java developers

  • mainly builds Java applications (also Kotlin, Groovy, C++)

Even though Gradle takes an opinionated view about how a Java project should be structured, it allows a large amount of customisation, including custom tasks, plugins, and multi-project builds. Gradle build scripts are written in either Groovy or Kotlin, so you can harness the power of a full JVM programming language during your build.

What is npm?

At its core, npm (Node Package Manager) manages packages for JavaScript applications. This includes both frontend applications running in the browser and backend server applications running in Node.js. To enable importing external packages into JavaScript files for code reuse, npm handles defining and fetching any required packages.

  • first released in 2010

  • used by over 11 million developers

  • builds JavaScript applications

All the packages that make up an npm project, also known as dependencies, are listed in a package JSON file. npm then fetches these packages from the central npm registry. They can then be used by any JavaScript code via an import statement.

Why compare Gradle & npm?

Gradle and npm are tools used to build two very different types of application. Gradle normally builds Java and is known as a build automation tool, whereas npm is JavaScript’s package manager.

While they have different use cases, the tools still have a lot of overlap. By understanding why one tool does something differently to the other, we can:

  • learn why each tool operates as it does

  • understand more deeply the target programming language

  • get into frontend / backend development

Similarities between Gradle & npm

Let’s first explore the areas common to both Gradle and npm.

Dependencies

One of the most obvious similarities is that both Gradle and npm manage dependencies. These are libraries or packages required to compile or run an application. Using dependencies is a developer’s way to avoid reinventing the wheel, by reusing code that someone else already wrote.

Gradle

In Gradle, dependencies are specified in the build.gradle build script file, using a group:name:version syntax. Here’s how to add a dependency on the Apache Commons Lang3 library.

dependencies {
    implementation 'org.apache.commons:commons-lang3:3.12.0'
}

With this in place, when you run ./gradlew build, Gradle downloads the dependency from the Maven Central Repository and stores it in a user-scoped cache in $USER_HOME/.gradle/caches. Gradle also ensures that the library is listed on the Java classpath, which means you can reference it from Java with an import statement.

import org.apache.commons.lang3.StringUtils;

Gradle won’t download the dependency next time you run ./gradlew build, since it’s already in the cache. Also, if another project needs the same dependency, it’s already available.

npm

npm dependencies are specified in package.json by including the name and version. Here’s how to add a dependency on the Chalk package.

  "dependencies": {
    "chalk": "^5.2.0"
  }

Now when you run npm install, npm downloads the dependency and stores it in a node_modules directory within your project directory. JavaScript code can then import the package as required.

import chalk from 'chalk';

npm is clever enough not to download the dependency again when you run npm install. But if you use the same dependency in another project, npm re-downloads it since node_modules is project-scoped.

Command line tool

Both tools have a command line tool which serves as the interface into the application whenever you need to build or run it.

Gradle

Gradle projects are normally bundled with the Gradle wrapper. It’s a script which downloads a specific version of the Gradle CLI. Using it means you don’t need a local Gradle installation.

The wrapper let’s you do five main things in a Java project:

  1. compile Java code (./gradlew compileJava)

  2. assemble jar files (./gradlew jar)

  3. run tests (./gradlew test)

  4. run the application (./gradlew run)

  5. troubleshoot (./gradlew dependencies)

To see a list of all available tasks, run ./gradlew tasks.

npm

To interact with JavaScript projects requires the npm command line tool to be installed. This is best done with a version manager like nvm or Nodist. Since npm runs on Node.js, the version manager also takes care of installing Node.js.

Important npm commands include:

  1. install all packages listed in package.json (npm install)

  2. run a script (npm run <script-name>)

  3. update package versions (npm update)

Other similarities

Other similarities between Gradle and npm are that they both support:

  • locking dependency versions

  • specifying dependency version ranges

  • separating production & development dependencies

  • publishing packages / libraries to a remote registry

  • hosting a private registry

Differences between Gradle & npm

Now let’s explore what separates Gradle and npm.

Compiling code

Since Gradle is the Java build tool, it has to handle complexities to do with compiling code, including Java versions and incremental compilation. On the other hand, JavaScript isn’t compiled, making npm simpler.

That said, npm can install packages which transpile code. For example , the @angular/cli package transpiles code from TypeScript to JavaScript.

Handling dependencies

We learned earlier that both Gradle and npm manage dependencies. But, due to how the Java classpath works, only one version of a Gradle dependency can be used during execution. Whereas in npm you can use multiple versions of a dependency within the same application, and even in the same JavaScript file.

Extensibility

Gradle and npm both offer extensibility, but in very different ways.

Gradle offers both core and 3rd party plugins, which you apply in the build script to add rich functionality. For example, the axion-release plugin adds functionality to determine semantic versions based on a Git repository’s tags.

In the npm world everything is a package. To add functionality to your project you install global packages, which can then be run from the command line. For example, when you install the @angular/cli package globally, you can build Angular applications with the ng build command.

Other differences

Here’s a summary of all the major differences between Gradle and npm, some of which were already mentioned:

Description Gradle npm
Local installation needed? No (if using wrapper) Yes
Runtime environment JVM Node.js
Project definition Groovy or Kotlin script JSON file
Compiles code? Yes No
Resolves multiple dependency versions? No Yes
Dependency cache User-scoped Project-scoped
Extensibility mechanism Plugins Packages

How to choose between Gradle & npm?

Since Gradle focuses on Java and npm on JavaScript, the choice between the two tools is determined by the type of application you need to build.

Choose Gradle if you need to build:

  • a backend Java application

  • an Android app

  • a reusable Java library

Choose npm if you need to bulid:

  • a frontend JavaScript application (e.g. using Angular, React, or Vue.js frameworks)

  • a backend Node.js service

  • a reusable JavaScript package

Interestingly, if you need to bundle an npm-built JavaScript application within a Java application, you can do that using the Gradle Plugin for Node. This is discussed in more detail in [What Java Developers Need To Know About NPM]({{relref “/what-java-developers-need-to-know-about-npm” }}).

Stop reading Gradle articles like these

This article helps you fix a specific problem, but it doesn't teach you the Gradle fundamentals you need to actually help your team succeed.

Instead, follow a step-by-step process that makes getting started with Gradle easy.

Download this Free Quick-Start Guide to building simple Java projects with Gradle.

  • Learn to create and build Java projects in Gradle.
  • Understand the Gradle fundamentals.