How to Configure Node.js in Jenkins for CI/CD
To configure
Node.js in Jenkins, first install the NodeJS plugin from the plugin manager. Then, add a Node.js installation in Jenkins global tools configuration and select it in your job to run Node.js commands.Syntax
Configuring Node.js in Jenkins involves these steps:
- Install NodeJS Plugin: Enables Node.js support in Jenkins.
- Global Tool Configuration: Define Node.js versions Jenkins can use.
- Job Configuration: Select the Node.js version and run Node.js commands.
bash
1. Go to Jenkins Dashboard > Manage Jenkins > Manage Plugins > Available 2. Search for "NodeJS" plugin and install it. 3. After restart, go to Manage Jenkins > Global Tool Configuration. 4. Scroll to NodeJS section and click Add NodeJS. 5. Enter a name and select the Node.js version or install automatically. 6. Save the configuration. 7. In your Jenkins job, under Build Environment, check "Provide Node & npm bin/ folder to PATH" and select the NodeJS installation. 8. Add build steps using Node.js commands like `npm install` or `node app.js`.
Example
This example shows how to configure a Jenkins freestyle job to run a Node.js script using the configured Node.js installation.
groovy
pipeline {
agent any
tools {
nodejs 'NodeJS_16' // Name of NodeJS installation in Global Tool Configuration
}
stages {
stage('Install Dependencies') {
steps {
sh 'npm install'
}
}
stage('Run Script') {
steps {
sh 'node index.js'
}
}
}
}Output
npm WARN deprecated ...
added 50 packages from 30 contributors and audited 50 packages in 2.5s
found 0 vulnerabilities
Hello from Node.js script!
Common Pitfalls
- Plugin Not Installed: Forgetting to install the NodeJS plugin causes Node.js options to be missing.
- Incorrect Node.js Version Name: Using a name in the job that doesn't match the global configuration.
- Not Adding Node.js to PATH: Missing the "Provide Node & npm bin/ folder to PATH" option leads to commands not found.
- Global Tool Configuration Not Saved: Changes not saved will not apply to jobs.
groovy
Wrong: // In pipeline, using wrong tool name nodejs 'NodeJS_14' Right: // Use exact name from Global Tool Configuration nodejs 'NodeJS_16'
Quick Reference
Summary tips for configuring Node.js in Jenkins:
- Always install the NodeJS plugin first.
- Define Node.js versions in Global Tool Configuration.
- Use exact Node.js installation names in your jobs.
- Enable "Provide Node & npm bin/ folder to PATH" in job settings.
- Test Node.js commands in a simple job before complex pipelines.
Key Takeaways
Install the NodeJS plugin in Jenkins to enable Node.js support.
Configure Node.js versions under Global Tool Configuration before using them in jobs.
Select the configured Node.js installation in your job and enable PATH provision.
Use exact names for Node.js installations to avoid configuration errors.
Test Node.js commands in Jenkins jobs to verify setup before full pipelines.