最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trying to deploy puppeteer on vercel using nodejs - Stack Overflow

programmeradmin1浏览0评论

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 /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 ChromeLauncherputeLaunchArguments (/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();

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.puteLaunchArguments (/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();
Share Improve this question asked Jan 21, 2024 at 16:01 Abhishek PoddarAbhishek Poddar 3192 gold badges7 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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.');

    result = await page.title();
  } catch (error) {
    return callback(error);
  } finally {
    if (browser !== null) {
      await browser.close();
    }
  }

  return callback(null, result);
};

My objective was transforme HTML to Pdf using puppeteer but I had errors in vercel when deploy.

This approach does work for me, I implemented Nestjs and Vercel application, you can optimize it with other approaches, hope it helps you. node >= v18

  • "puppeteer": "^22.11.0",
  • "puppeteer-core": "^22.11.0",
  • "@sparticuz/chromium": "^123.0.0",

info support chromium with puppeteer

import { Logger } from '@nestjs/mon';

import * as puppeteer from 'puppeteer';

import chromium from '@sparticuz/chromium';
import puppeteerCore from 'puppeteer-core';

export const getPdf = async (
  html: any
  htmlFileName: string,
): Promise<Buffer> => {
  let browser = null;

  if (process.env.NODE_ENV === 'development') {
    Logger.log('Development browser: ');
    browser = await puppeteer.launch({
      args: ['--no-sandbox', '--disable-setuid-sandbox'],
      headless: true,
    });
  }
  if (process.env.NODE_ENV === 'production') {
    Logger.log('Development production: ');
    browser = await puppeteerCore.launch({
      args: chromium.args,
      defaultViewport: chromium.defaultViewport,
      executablePath: await chromium.executablePath(),
      headless: chromium.headless,
    });
  }
  const page = await browser.newPage();
  await page.setContent(html, { waitUntil: 'networkidle0' });
  const pdfBuffer = await page.pdf();
  await browser.close();
  return pdfBuffer;
};

发布评论

评论列表(0)

  1. 暂无评论