javascriptnode.jspdfpuppeteervercel

Trying to deploy puppeteer on vercel using nodejs


I am trying to deploy my script on vercel using nodejs.

node v - 18x ppuppeteer v - 21.7.0 Could not find Chrome (ver. 119.0.6045.105)

when I try to execute the script it gives me an error :

Error: Could not find Chrome (ver. 119.0.6045.105). This can occur if either
 1. you did not perform an installation before running the script (e.g. `npx puppeteer browsers install chrome`) or
 2. your cache path is incorrectly configured (which is: /home/sbx_user1051/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
    at ChromeLauncher.resolveExecutablePath (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:278:27)
    at ChromeLauncher.executablePath (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:209:25)
    at ChromeLauncher.computeLaunchArguments (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:103:37)
    at async ChromeLauncher.launch (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:69:28)
    at async way_bill_generate (/var/task/controller/orders/orders.js:784:21)

code :

   options = {
      headless: true,
      // executablePath: '/usr/bin/chromium-browser',
      args: [
        "--no-sandbox",
        "--disable-setuid-sandbox",
        "--disable-web-security",
        "--hide-scrollbars",
        "--font-render-hinting=none",
      ],
    }

const browser = await puppeteer.launch(options);

const page = await browser.newPage();
await page.setContent(html)

await page.emulateMediaType("screen");
    await page.setViewport({
      width: 1200,
      height: 800,
    });


    const pdfBuffer = await page.pdf({
      margin: { top: "50px", right: "50px", bottom: "50px", left: "50px" },
      printBackground: true,
      preferCSSPageSize: true,
      format: "A4",
    });

    await browser.close();

Solution

  • After so many research I got the solution. If you want to use Puppeteer with serverless architecture I found this official git repo . This is only supported specified version of chromium with particular puppeteer version. To check check which version you should use support check .

    or if Migration from chrome-aws-lambda

    const chromium = require("@sparticuz/chromium");
    const puppeteer = require("puppeteer-core");
    
    exports.handler = async (event, context, callback) => {
      let result = null;
      let browser = null;
    
      try {
        browser = await puppeteer.launch({
          args: chromium.args,
          defaultViewport: chromium.defaultViewport,
          executablePath: await chromium.executablePath(),
          headless: chromium.headless,
          ignoreHTTPSErrors: true,
        });
    
        let page = await browser.newPage();
    
        await page.goto(event.url || 'https://example.com');
    
        result = await page.title();
      } catch (error) {
        return callback(error);
      } finally {
        if (browser !== null) {
          await browser.close();
        }
      }
    
      return callback(null, result);
    };