Skip to main content
In this lesson we update a Jenkins pipeline to run unit tests across multiple Node.js versions using parallel stages. You’ll learn why a parallel stage may fail with “mocha: not found” and how to fix it by ensuring dependencies are available to each runtime.

Pipeline context

The pipeline runs on a Kubernetes pod whose default container is node-18. The tools stanza references a named NodeJS tool configured in Jenkins global tools.

Where dependencies are installed

Below is a fuller pipeline excerpt showing an Installing Dependencies stage and a placeholder for dependency scanning. This illustrates the typical place to run npm install before tests.

Parallelizing unit tests across Node.js versions

You can run tests in parallel for multiple Node.js versions. The example below runs Node.js 18 and 19 as containers inside the same Kubernetes pod, and Node.js 20 using a Docker agent on the Jenkins agent host.
Important distinctions:
  • NodeJS 18 and NodeJS 19 run in containers that are part of the same Kubernetes pod. They may share the same underlying workspace depending on how the pod/agent is configured.
  • NodeJS 20 runs in a separate Docker agent. It typically does not share the same workspace or filesystem with the Kubernetes pod containers.
When parallel stages use different agent types (Kubernetes containers vs Docker agent), they generally do not share the same filesystem or installed dependencies. Ensure each runtime has access to dependencies by running npm install inside that stage, or by using stash/unstash when the agent/workspace mechanisms permit it.

Example failure: “mocha: not found”

When Node 20 runs in its Docker agent, you might see this console output:
Why this happens:
  • npm install was executed inside the Kubernetes pod during Installing Dependencies, so node_modules were installed in that pod’s workspace.
  • The NodeJS 20 stage runs in a separate Docker agent (node:20-alpine) which does not have access to the same node_modules and therefore cannot find mocha.

Solutions

You have two common, reliable options:
  1. Install dependencies inside each parallel stage that needs them (recommended for portability).
  2. Use stash/unstash to transfer a prepared node_modules or workspace between stages — only works when the Jenkins stash/un-stash mechanism and agent types allow it.
Below are code examples for both approaches.

A — Install dependencies inside the NodeJS 20 stage

Add npm install to the stage running under the Docker agent:
This is straightforward and avoids cross-platform binary issues.

B — Stash/Unstash dependencies (when appropriate)

If both the producer and consumer stages can use Jenkins’ stash/unstash workspace mechanism, you can prepare dependencies once and then unstash in the Docker agent:
Stashing node_modules can transfer platform-specific binaries that may be incompatible with the target runtime (for example, Debian/glibc binaries vs Alpine/musl). If native addons or compiled binaries are present, prefer running npm install in the target runtime or building artifacts in a consistent environment.

Comparison: approaches at a glance

Summary

  • Use parallel stages to test across multiple Node.js versions.
  • Always be aware where npm install runs — different agents/containers do not necessarily share the same filesystem.
  • To fix errors like mocha: not found, either install dependencies inside the failing stage or use stash/unstash when agent/workspace compatibility permits.
  • Prefer installing in the target runtime when native modules or platform-specific binaries exist.

Watch Video