Set Chrome Bin for Karma Headless Chrome CI
In this article
Setting the Chrome binary path for Karma tests in a headless Chrome CI environment ensures your tests run consistently using the correct Chrome version, especially when default installations aren’t detected. This is essential in CI/CD pipelines where Chrome may not be installed in standard locations or when using custom or managed Chrome builds. By explicitly defining the Chrome binary path in your Karma configuration, you avoid execution failures due to missing or mismatched browsers. This guide walks you through configuring Karma to use a specific Chrome executable in headless mode on continuous integration systems like GitHub Actions, GitLab CI, or Jenkins.
What You'll Need
- A working Karma configuration file (karma.conf.js)
- Chrome or Chromium installed in your CI environment (or via a package like
puppeteer) - Access to modify CI environment variables or install packages (e.g., via
.gitlab-ci.yml,github/workflows/test.yml, etc.) - Node.js and npm or yarn installed in the CI environment
- Basic familiarity with command-line tools and JavaScript configuration
Step-by-Step
- Install Chrome or Chromium in your CI environment. Many CI platforms don’t include Chrome by default. Use a package manager to install it. For example, in a GitHub Actions workflow, add:
Or, if using Puppeteer (which bundles Chromium), install it via:- name: Install Chrome run: sudo apt-get update && sudo apt-get install -y google-chrome-stablenpm install puppeteer --save-dev - Determine the Chrome binary path. Find where Chrome is installed. Common paths include:
- Linux:
/usr/bin/google-chromeor/usr/bin/chromium-browser - macOS:
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome - CI with Puppeteer:
node_modules/puppeteer/.local-chromium/(path varies by OS)
const puppeteer = require('puppeteer'); console.log(puppeteer.executablePath()); - Linux:
- Modify your Karma configuration file (karma.conf.js). Open
karma.conf.jsand locate thebrowsersandcustomLauncherssections. If they don’t exist, add them. Define a custom launcher for Headless Chrome with the explicit binary path:customLaunchers: { ChromeHeadlessCI: { base: 'ChromeHeadless', binary: '/usr/bin/google-chrome', // Update with actual path flags: [ '--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--disable-accelerated-2d-canvas', '--no-first-run', '--no-zygote', '--single-process', // Optional: helps in some CI environments ] } }, browsers: ['ChromeHeadlessCI'] - Use environment variables for flexibility. Instead of hardcoding the path, use
process.envto make it configurable:
Then, in your CI script, set the environment variable before running Karma:binary: process.env.CHROME_BIN || '/usr/bin/google-chrome'CHROME_BIN=/usr/bin/google-chrome npm run test - Update CI pipeline script. In your CI configuration (e.g.,
.github/workflows/test.yml), ensure the test script sets the environment and runs Karma. Example:
Where- name: Run Karma Tests env: CHROME_BIN: /usr/bin/google-chrome run: npm run test:citest:ciis an npm script likekarma start karma.conf.js --single-run. - Verify Chrome launches correctly. Run the Karma command in your CI job and check logs. Look for messages like
ChromeHeadlessCI has connectedand ensure noChrome not founderrors appear. - Handle platform-specific paths (optional). If your project runs on multiple CI platforms, conditionally set the binary path:
binary: process.platform === 'linux' ? process.env.CHROME_BIN || '/usr/bin/chromium-browser' : '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
Common Mistakes
- Using
ChromeHeadlesswithout settingno-sandboxflags — causes crashes in CI due to restricted environments. - Hardcoding binary paths that don’t exist in the CI runner (e.g., macOS path on Linux CI).
- Forgetting to install Chrome in the CI environment, leading to “command not found” errors.
- Omitting
--disable-dev-shm-usage, which can cause memory issues in Docker-based CI runners. - Not updating
browsersarray to use the custom launcher name.
Tips
- Use Puppeteer to manage Chromium — it guarantees a compatible, known version and provides the executable path programmatically.
- Log the Chrome version in CI:
$(which google-chrome) --versionhelps debug version mismatches. - Reuse the same Chrome instance flags across projects to maintain consistency.
- Set
single-processflag cautiously — it can help in constrained environments but may hide issues. - Test locally with the same flags to catch config issues early.
Troubleshooting
If Karma fails to launch Chrome:
- “Chrome not found” error: Verify Chrome is installed and the binary path is correct. Run
which google-chromeorwhich chromium-browserin CI to confirm. - Chrome crashes on launch: Add
--no-sandboxand--disable-setuid-sandboxflags. These are often required in containerized environments. - Tests hang or timeout: Include
--disable-dev-shm-usageand--no-zygoteto reduce memory pressure. - Wrong Chrome version used: Explicitly install the desired version in CI and verify via
google-chrome --version. - Environment variable not recognized: Ensure
CHROME_BINis exported in the same shell session as Karma. Useenv:in CI config or prefix the command.
Frequently Asked Questions
Why do I need to set CHROME_BIN?
Karma may not detect Chrome automatically in CI environments. Setting CHROME_BIN ensures Karma uses the correct executable, especially when Chrome is installed in non-standard paths.
Can I use Chromium instead of Chrome?
Yes. Use the Chromium binary path (e.g., /usr/bin/chromium-browser) and ensure it’s installed in CI. Many Linux distributions package Chromium by default.
What flags are required for headless Chrome in CI?
Essential flags include --no-sandbox, --disable-setuid-sandbox, and --disable-dev-shm-usage. These prevent crashes in restricted environments like Docker.
How do I find the Chrome binary path in Puppeteer?
Run require('puppeteer').executablePath() in a Node script. This returns the full path to the bundled Chromium executable.
Does this work with Docker?
Yes. Ensure Chrome is installed in the Docker image and the binary path matches. Use the same flags and environment variables as in CI.
Can I run this on GitHub Actions and GitLab CI?
Yes. The configuration is platform-agnostic. Just ensure Chrome is installed and the binary path is correct for the runner’s OS.