R
Railway•11mo ago
Paul

puppeteer: Failed to launch the browser process!

Hi, i am using puppeteer to create PDF from HTML. The goal is to generate invoices & quotes from an html page. The API works fine, but every time i'm trying to create a PDF i encounter the following error:
[Nest] 19 - 01/17/2024, 2:55:55 PM ERROR [ExceptionsHandler] Failed to launch the browser process!

/home/node/.cache/puppeteer/chrome/linux-119.0.6045.105/chrome-linux64/chrome: error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory

TROUBLESHOOTING: https://pptr.dev/troubleshooting

at Interface.onClose (/usr/src/app/node_modules/@puppeteer/browsers/lib/cjs/launch.js:277:24)
at Interface.emit (node:events:529:35)
at Interface.emit (node:domain:489:12)
at Interface.close (node:internal/readline/interface:534:10)
at Socket.onend (node:internal/readline/interface:260:10)
at Socket.emit (node:events:529:35)
at Socket.emit (node:domain:489:12)
at endReadableNT (node:internal/streams/readable:1400:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
[Nest] 19 - 01/17/2024, 2:55:55 PM ERROR [ExceptionsHandler] Failed to launch the browser process!

/home/node/.cache/puppeteer/chrome/linux-119.0.6045.105/chrome-linux64/chrome: error while loading shared libraries: libgobject-2.0.so.0: cannot open shared object file: No such file or directory

TROUBLESHOOTING: https://pptr.dev/troubleshooting

at Interface.onClose (/usr/src/app/node_modules/@puppeteer/browsers/lib/cjs/launch.js:277:24)
at Interface.emit (node:events:529:35)
at Interface.emit (node:domain:489:12)
at Interface.close (node:internal/readline/interface:534:10)
at Socket.onend (node:internal/readline/interface:260:10)
at Socket.emit (node:events:529:35)
at Socket.emit (node:domain:489:12)
at endReadableNT (node:internal/streams/readable:1400:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
And the only related code in my project is
import * as puppeteer from 'puppeteer';

const defaultOptions = {
format: 'A4',
printBackground: true,
} as puppeteer.PDFOptions;

export class PDFService {
constructor() {}

async pageToBuffer(path: string, options = defaultOptions) {
const browser = await puppeteer.launch({
headless: 'new',
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
],
});

const page = await browser.newPage();
await page.goto(`${process.env.API_URL}/${path}`, {
waitUntil: 'networkidle0',
});
const pdf = await page.pdf(options);
await browser.close();
return pdf;
}
}
import * as puppeteer from 'puppeteer';

const defaultOptions = {
format: 'A4',
printBackground: true,
} as puppeteer.PDFOptions;

export class PDFService {
constructor() {}

async pageToBuffer(path: string, options = defaultOptions) {
const browser = await puppeteer.launch({
headless: 'new',
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
],
});

const page = await browser.newPage();
await page.goto(`${process.env.API_URL}/${path}`, {
waitUntil: 'networkidle0',
});
const pdf = await page.pdf(options);
await browser.close();
return pdf;
}
}
I'm using Nest.js. Thanks in advance project id: 245b1448-066b-4ded-9cac-b414f50b53fc
Solution:
thank you @Brody for your help ! The solution was to add chrome in the dockerfile and change the PUPPETEER env variable ```FROM node:18-alpine as builder ...
Jump to solution
7 Replies
Percy
Percy•11mo ago
Project ID: 245b1448-066b-4ded-9cac-b414f50b53fc
Paul
PaulOP•11mo ago
<:blob_help:1149662715409874944>
Brody
Brody•11mo ago
try adding a nixpacks.toml to your project with this in it
[phases.setup]
aptPkgs = [
'...',
'libnss3',
'libatk1.0-0',
'libatk-bridge2.0-0',
'libcups2',
'libgbm1',
'libasound2',
'libpangocairo-1.0-0',
'libxss1',
'libgtk-3-0',
'libxshmfence1',
'libglu1'
]
[phases.setup]
aptPkgs = [
'...',
'libnss3',
'libatk1.0-0',
'libatk-bridge2.0-0',
'libcups2',
'libgbm1',
'libasound2',
'libpangocairo-1.0-0',
'libxss1',
'libgtk-3-0',
'libxshmfence1',
'libglu1'
]
Paul
PaulOP•11mo ago
i still have the same error, do i have to specify something in railway? i use a dockerfile to build the api
Brody
Brody•11mo ago
then you need to install those apt packages with your dockerfile
Paul
PaulOP•11mo ago
ok thank you i'm gonna try that and lyk 🙂
Solution
Paul
Paul•11mo ago
thank you @Brody for your help ! The solution was to add chrome in the dockerfile and change the PUPPETEER env variable
FROM node:18-alpine as builder

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json yarn.lock ./

RUN yarn install

COPY . .

RUN yarn build

FROM node:18-alpine

RUN apk add --no-cache chromium

USER node
# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json yarn.lock ./

# Tell Puppeteer to skip installing Chrome. We'll be using the installed package.
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
ENV PUPPETEER_CACHE_DIR=/home/web/.cache

RUN yarn install

COPY --from=builder /usr/src/app/dist ./dist

CMD [ "node", "dist/main.js" ]
FROM node:18-alpine as builder

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json yarn.lock ./

RUN yarn install

COPY . .

RUN yarn build

FROM node:18-alpine

RUN apk add --no-cache chromium

USER node
# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json yarn.lock ./

# Tell Puppeteer to skip installing Chrome. We'll be using the installed package.
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
ENV PUPPETEER_CACHE_DIR=/home/web/.cache

RUN yarn install

COPY --from=builder /usr/src/app/dist ./dist

CMD [ "node", "dist/main.js" ]
Want results from more Discord servers?
Add your server