Pipeline context
The pipeline runs on a Kubernetes pod whose default container isnode-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 anInstalling 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.NodeJS 18andNodeJS 19run 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 20runs 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:npm installwas executed inside the Kubernetes pod duringInstalling Dependencies, sonode_moduleswere installed in that pod’s workspace.- The
NodeJS 20stage runs in a separate Docker agent (node:20-alpine) which does not have access to the samenode_modulesand therefore cannot findmocha.
Solutions
You have two common, reliable options:- Install dependencies inside each parallel stage that needs them (recommended for portability).
- Use
stash/unstashto transfer a preparednode_modulesor workspace between stages — only works when the Jenkins stash/un-stash mechanism and agent types allow it.
A — Install dependencies inside the NodeJS 20 stage
Addnpm install to the stage running under the Docker agent:
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 installruns — 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.
Links and references
- Jenkins Pipeline Syntax
- Kubernetes Concepts
- Node.js Docker images
- Jenkins Stash/Unstash documentation (Pipeline)