javascriptnestjspuppeteer

/usr/bin/chromium-browser NOENT error arises when calling puppeteer.launch


I am working on a project with Nest.js with a feature to generate PDF document. I am using puppeteer library to generate the PDF by the API https://pptr.dev/api/puppeteer.page.pdf

It works well locally with the docker OS as Ubuntu. The chromium path I set is /usr/bin/chromium-browser.

However, when I deploy to production with docker OS as node:20.18, seems like the setting does not work as /usr/bin/chromium-browser NOENT error is found in the log.

The codes to initiate the puppeteer browser:

export class BrowserService implements OnModuleInit, OnModuleDestroy {
    private browser: puppeteerCore.Browser | Browser;

    async onModuleInit() {
        if (this.browser) return;
        const puppeteer = process.env.NODE_ENV === 'local' ? puppeteerFull : puppeteerCore;
        this.browser = await puppeteer.launch({
            executablePath: process.env.NODE_ENV === 'local' ? puppeteer.executablePath() : '/usr/bin/chromium-browser',
            headless: true,
            args: ['--no-sandbox', '--disable-setuid-sandbox'],
        });
    }

    getBrowser() {
        return this.browser;
    }
}

The nest application server log shows the NOENT error as below:

Error: Failed to launch the browser process! spawn /usr/bin/chromium-browser ENOENT
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/usr/src/app/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:299:20)
at ChildProcess.<anonymous> (/usr/src/app/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:293:24)
at ChildProcess.emit (node:events:519:28)
at ChildProcess.emit (node:domain:488:12)
at ChildProcess._handle.onexit (node:internal/child_process:292:12)
at onErrorNT (node:internal/child_process:484:16)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
WARN received SIGTERM indicating exit request
INFO waiting for 01-node_00, 02-java_00 to die
WARN stopped: 02-java_00 (exit status 143)
WARN stopped: 01-node_00 (terminated by SIGTERM)

Anyone has idea how to fix this issue?

The difference on docker OS is the only reason I can think of. Appreciate if anyone can give me some guidance.


Solution

  • I think the executablePath should be adjusted according to the host OS within the docker container.

    '/usr/bin/chromium-browser' will work for Ubuntu OS.

    For node:20.18, it should be an alpine OS, so may be you can try to change the executablePath to '/usr/bin/chromium'?