Fix Node.js Puppeteer Launch Problem on M1 Macs with Docker

Fix Node.js Puppeteer Launch Problem on M1 Macs with Docker Image

I am the only one who develops on Macbook Pro with an M1 (Silicon) Chip at the company I have been working for. That’s why the problem was raised when I tried to run Puppeteer on my Mac. With the help of one of my colleagues — thanks to Rhodri —, I succeeded to solve the problem.

Why does this happen?

When you try to build a Docker image that includes a Puppeteer installation using npm or yarn, it might fail to run if you are using an M1 Macbook. It is just because of the CPU architecture (x64 M1). It downloads a local version of Chromium successfully, but cannot run it when you try to do so, and generally, it throws an error as follows:

Failed to launch the browser process!

How to fix it?

You need to tell Puppeteer that you are going to install Chromium manually, and it should not install any local version of Chromium. In the Dockerfile you have, add the following environment variables:

# Have Puppeteer skip the local Chromium download step

# The path where we install Chromium manually

Then, add the following line to the Dockerfile that allows you to download the Chromium:

RUN apt-get update && apt-get install chromium

That’s it. It should now work as expected without any errors.

One thing to remember is that the Chromium installation command above might change according to the image you use in Dockerfile. To illustrate, although Debian-based images use “apt” as the package manager, Alpine uses “apk”. So, if you use an Alpine-based image, in such a case, you would add the following line to your Dockerfile:

RUN apk update && apk add chromium