pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
14.95k stars 3.55k forks source link

Puppeteer error using docker and railway #2057

Closed NicolasVillalobos closed 8 months ago

NicolasVillalobos commented 1 year ago

Is there an existing issue for this?

Describe the bug

(node:17) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process! spawn /app/node_modules/puppeteer/.local-chromium/linux-982053/chrome-linux/chrome ENOENT TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md at onClose (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241:20) at ChildProcess. (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:233:85) at ChildProcess.emit (events.js:400:28) at Process.ChildProcess._handle.onexit (internal/child_process.js:283:12) at onErrorNT (internal/child_process.js:472:16) at processTicksAndRejections (internal/process/task_queues.js:82:21) (Use node --trace-warnings ... to show where the warning was created) (node:17) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:17) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Expected behavior

use whatsapp-web.js in railway with docker

Steps to Reproduce the Bug or Issue

Use Dockerfile

FROM node:14-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD [ "npm", "start" ]

Relevant Code

const puppeteerOptions = { puppeteer: { args: ['--no-sandbox', '--disable-setuid-sandbox'] } };

const client = new Client(puppeteerOptions);

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

node:14-alpine

Additional context

No response

alamia442 commented 1 year ago

Same here, any updates on the problem? :(

NicolasVillalobos commented 1 year ago

I asked a railway moderator and he told me the following:

whatsapp-web bots are considered userbots and thus arent allowed on railway, sorry https://railway.app/legal/fair-use

ivanzigoni commented 1 year ago

this is not a bug, you just need to install chrome on your container.

search for how to install chrome on a docker image

NicolasVillalobos commented 1 year ago

@alamia442 try this in the dockerfile:

FROM alpine

# Installs latest Chromium (100) package.
RUN apk add --no-cache \
      chromium \
      nss \
      freetype \
      harfbuzz \
      ca-certificates \
      ttf-freefont \
      nodejs \
      npm \
      yarn

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

# Puppeteer v13.5.0 works with Chromium 100.
RUN yarn add puppeteer@13.5.0

# Add user so we don't need --no-sandbox.
RUN addgroup -S pptruser && adduser -S -G pptruser pptruser \
    && mkdir -p /logs \
    && chown -R pptruser:pptruser /logs \
    && mkdir -p /home/pptruser/Downloads /app \
    && chown -R pptruser:pptruser /home/pptruser \
    && chown -R pptruser:pptruser /app

# Run everything after as non-privileged user.
USER pptruser

EXPOSE 3500
COPY . .
CMD ["npm", "run", "start"]
LucaTrip commented 1 year ago

Thanks @NicolasVillalobos with this Dockerfile the Puppeteer error gone away but now I have another error. I premise that I am not very confident with docker but when I deploy my nodejs app on Railway.app I have this error:

Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted
[0807/172929.048180:FATAL:zygote_host_impl_linux.cc(201)] Check failed: . : Operation not permitted (1)
[0100/000000.120794:ERROR:zygote_linux.cc(661)] write: Broken pipe (32)
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/usr/src/app/node_modules/puppeteer/src/node/BrowserRunner.ts:268:9)
at Interface.<anonymous> (/usr/src/app/node_modules/puppeteer/src/node/BrowserRunner.ts:254:50)
at Interface.emit (node:events:526:35)
at Interface.emit (node:domain:489:12)
at Interface.close (node:internal/readline/interface:533:10)
at Socket.onend (node:internal/readline/interface:259:10)
at Socket.emit (node:events:526:35)
at Socket.emit (node:domain:489:12)
at endReadableNT (node:internal/streams/readable:1359:12)
at processTicksAndRejections (node:internal/process/task_queues:82:21)

My Dockerfile is this


# Installs latest Chromium (100) package.
RUN apk add --no-cache \
      chromium \
      nss \
      freetype \
      harfbuzz \
      ca-certificates \
      ttf-freefont \
      nodejs \
      npm \
      yarn

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

# Puppeteer v13.5.0 works with Chromium 100.
RUN yarn add puppeteer@13.5.0

# Add user so we don't need --no-sandbox.
RUN addgroup -S pptruser && adduser -S -G pptruser pptruser \
    && mkdir -p /logs \
    && chown -R pptruser:pptruser /logs \
    && mkdir -p /home/pptruser/Downloads /app \
    && chown -R pptruser:pptruser /home/pptruser \
    && chown -R pptruser:pptruser /app

# Run everything after as non-privileged user.
USER pptruser

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install

EXPOSE 3500
COPY . .
CMD ["npm", "run", "start:dev"]

Do you have some ideas?

ivanzigoni commented 1 year ago

@LucaTrip

try this

FROM alpine

# Installs latest Chromium (100) package.
RUN apk add --no-cache \
      chromium \
      nss \
      freetype \
      harfbuzz \
      ca-certificates \
      ttf-freefont \
      nodejs \
      npm \
      yarn

# 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

WORKDIR /usr/src/app

COPY . .

RUN npm install

CMD ["npm", "start"]

and

const client = new Client({
    puppeteer: { args: ["--no-sandbox", "--disable-dev-shm-usage"] },
    authStrategy: new LocalAuth()
});

worked fine for me last time i tried =)

LucaTrip commented 1 year ago

Thanks @ivanzigoni the error is gone away, but in this way doesn't generete to me the qr code because I have the necessity to generete qr code on server side.

ivanzigoni commented 1 year ago

@LucaTrip couldnt understand what you mean. the qr code string is generated as usual and sent to the event handler.

LucaTrip commented 1 year ago

@ivanzigoni sorry for my bad explanation. I was trying to say that with "--no-sandbox" arg for puppeteer the library whatsapp-web.js doesn't generete the qr code or issue me any event in general. I don't know why

AnthonyLzq commented 1 year ago

@LucaTrip

try this

FROM alpine

# Installs latest Chromium (100) package.
RUN apk add --no-cache \
      chromium \
      nss \
      freetype \
      harfbuzz \
      ca-certificates \
      ttf-freefont \
      nodejs \
      npm \
      yarn

# 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

WORKDIR /usr/src/app

COPY . .

RUN npm install

CMD ["npm", "start"]

and

const client = new Client({
    puppeteer: { args: ["--no-sandbox", "--disable-dev-shm-usage"] },
    authStrategy: new LocalAuth()
});

worked fine for me last time i tried =)

This solution fixed the issue for me!

LucaTrip commented 1 year ago

@AnthonyLzq it's works for me in local, but when I deploy it on Railway doesn't generate the qrcode