Jenkins pipeline failing to set up Chrome headless for Puppeteer

I’m having trouble with my Jenkins pipeline after upgrading my Angular project to use Node 18. Everything works fine locally but the pipeline breaks when trying to run tests.

The error message says it can’t find the Chrome binary:

Cannot start ChromeHeadless
Can not find the binary /home/ec2-user/.cache/puppeteer/chrome/linux-134.0.6998.35/chrome-linux64/chrome
Please set env variable CHROME_BIN

I’ve tried updating Puppeteer and tweaking my karma.conf.ci.js file. It includes setting CHROME_BIN which works for other projects:

process.env.CHROME_BIN = require('puppeteer').executablePath()

The config also has custom launcher settings:

customLaunchers: {
  ChromeHeadlessCI: {
    base: 'ChromeHeadless',
    flags: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
  },
},

Any ideas on how to fix this? I’m stumped!

hey, i had a similar issue. try adding this to ur pipeline:

sh 'npm install puppeteer'
sh 'node -e \"console.log(require('puppeteer').executablePath())\"`

this installs puppeteer and logs the chrome path. make sure to set CHROME_BIN to that path in ur karma config. also check if jenkins has permissions to access that directory. good luck!

I managed to overcome this issue by ensuring that Chromium was explicitly installed in the CI environment, which avoided the pitfalls of Puppeteer not automatically setting up Chrome in our Node 18 setup.

For our Jenkins pipeline, we modified the Jenkinsfile to install Chromium manually:

sh 'apt-get update && apt-get install -y chromium-browser'
sh 'export CHROME_BIN=/usr/bin/chromium-browser'

and then in karma.conf.ci.js, we set:

process.env.CHROME_BIN = '/usr/bin/chromium-browser'

This approach ensured that the correct binary was available, and that the environment variable CHROME_BIN was properly defined. Also, verifying that the Node version in the CI environment matches the local version helped avoid similar issues.

Have you considered using a Docker container for your Jenkins pipeline? This approach solved a similar issue for me recently. By creating a custom Dockerfile that includes both Node 18 and Chrome, you can ensure a consistent environment across local and CI setups.

In the Dockerfile, you’d include something like:

FROM node:18
RUN apt-get update && apt-get install -y chromium
ENV CHROME_BIN=/usr/bin/chromium

Then in your Jenkinsfile, you’d use this Docker image for your pipeline steps. This way, you’re guaranteed to have Chrome available and correctly referenced by CHROME_BIN.

Also, double-check your Puppeteer version is compatible with Node 18. Upgrading Puppeteer might resolve dependency conflicts causing the Chrome binary detection to fail.