Discover why `npm` commands fail on Jenkins agents and learn how to set up Node.js in your Jenkins pipeline correctly.
---
This video is based on the question https://stackoverflow.com/q/68169605/ asked by the user 'Seth Lutske' ( https://stackoverflow.com/u/12010984/ ) and on the answer https://stackoverflow.com/a/68169745/ provided by the user 'Dmitriy Tarasevich' ( https://stackoverflow.com/u/13908508/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: npm: not found on jenkins agent, but available through ssh
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the Jenkins npm: not found Issue
If you've been running into the frustrating issue of receiving npm: not found while executing commands in a Jenkins pipeline, you're not alone. Many users face this challenge, especially when working with multiple agents, such as Ubuntu instances in cloud environments. The problem arises despite the fact that npm works perfectly fine when accessed directly via SSH.
In this guide, we'll delve into the core of the issue and provide a step-by-step solution to ensure that your Jenkins pipeline can access npm seamlessly across all agents.
The Problem
You've set up a Jenkins pipeline that utilizes agents on Ubuntu instances but encountered an error when trying to run npm commands.
Agents Configuration: You have two nodes (agent1 and agent2), both accessible to a user (cooluser1) via SSH.
Successful SSH Access: Using SSH, you can successfully run npm -v and get the expected output.
Pipeline Failure: When running the same command in your Jenkins pipeline, you receive the error: npm: not found.
This suggests there is a discrepancy between your terminal environment and the Jenkins agents’ environment settings.
Why Does This Happen?
The issue stems from how Jenkins sets up the environment for different agents. When you run commands manually through SSH, they inherit the user’s environment, including paths set in .bashrc or .bash_profile. Jenkins, however, may not load these files, leading to missing command paths.
The Solution: Installing Node.js Plugin in Jenkins
To resolve this issue, we can make use of the Jenkins Node.js plugin. This plugin allows the configuration of different versions of Node.js which are then made available for use in your pipeline scripts. Follow these steps:
Step 1: Install the Node.js Plugin
Open your Jenkins dashboard.
Navigate to Manage Jenkins.
Select Manage Plugins.
Under the Available tab, search for "NodeJS Plugin" and install it.
Step 2: Configure Node.js in Jenkins
After installation, head to Manage Jenkins - Global Tool Configuration.
Scroll down to the NodeJS section.
Click Add NodeJS.
Give it a name (e.g., NodeJS_14.17.1).
Select the version you wish to install (make sure it matches the version on your agents).
Save the configuration.
Step 3: Modify Your Pipeline Script
Update your Jenkins pipeline script to use the Node.js tool you've just configured:
[[See Video to Reveal this Text or Code Snippet]]
Step 4: Run Your Pipeline
With the above changes, your Jenkins pipeline will now have access to the npm command, and your jobs involving Node.js will run smoothly across all configured agents.
Conclusion
The npm: not found error might seem daunting, but by ensuring that your Jenkins pipeline has the right configuration and access to Node.js, you can eliminate this issue. The key takeaway is to utilize the Node.js Plugin in Jenkins for a cleaner, more reliable build environment.
Incorporating these changes will not only resolve your current issue but also enhance your workflow for future projects involving Node.js in Jenkins.
Happy coding!
npm: not found on jenkins agent but available through sshlinuxjenkinsnpmjenkins pipeline