在Github Actions的ubuntu-latest(23)环境下使用Puppeteer出现“No usable sandbox”错误
Let's break down why you're hitting this sandbox error even after setting --no-sandbox—and fix it step by step.
The Core Issue
Your Karma config defines a custom ChromeHeadlessCustom launcher with the --no-sandbox flag, but your test command is using the default ChromeHeadless launcher (which doesn't include that flag). That's why the sandbox restriction is still kicking in.
Step 1: Use Your Custom Launcher in the Test Command
Update your test:ci script to target the custom launcher you created:
"test:ci": "ng test ngx-float-ui --browsers=ChromeHeadlessCustom --watch=false"
Step 2: (Optional) Clean Up Your Browsers List
You have ChromeHeadlessNoSandbox listed in your browsers array, but you never defined that launcher. If you prefer that name, adjust your config like this:
customLaunchers: { ChromeHeadlessNoSandbox: { // Rename to match your browsers list base: "ChromeHeadless", flags: [ "--no-sandbox", "--disable-gpu", "--remote-debugging-port=9333" ] } }
Then update your test command to use this name instead:
"test:ci": "ng test ngx-float-ui --browsers=ChromeHeadlessNoSandbox --watch=false"
Step 3: Add Missing Chrome Dependencies to Github Actions
Github Actions' Ubuntu runners often lack some system libraries required by ChromeHeadless. Add this step to your workflow file before running tests:
- name: Install Chrome system dependencies run: | sudo apt-get update sudo apt-get install -y libnss3 libx11-xcb1 libxcb-dri3-0 libdrm2 libgbm1 libasound2
This ensures all required libraries are present for Chrome to launch successfully.
Step 4: Verify Puppeteer & Chrome Compatibility
Occasionally, version mismatches between Puppeteer and the Chrome binary it downloads can cause issues. If the above steps don't work, try:
- Locking your Puppeteer version to a stable release (check the Puppeteer docs for compatible Chrome versions)
- Using
puppeteer-coreinstead ofpuppeteerto use the system-installed Chrome from your Github Actions runner
内容的提问来源于stack exchange,提问作者OtaconKiko

