If we want to use Puppeteer in nodejs, we simply do a:
npm i puppeteer
# or "yarn add puppeteer"
After that, humans would think that the following code would work:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
Well…let me tell you, it will work…but not on an ARM Processor, and we’re so lucky today, because Raspberry Pi contains an ARM Processor.
You would get the following error trying to run:
Error: Failed to launch chrome!
/home/xxxx/.../node_modules/puppeteer/.local-chromium/liniux-641577/chrome-linux/chrome: Syntax error: word unexpected (expecting ") "
Solving the issue
If you are running an ARM processor, e.g. Raspberry PI, as stated above, I solved the issue with the following command:
Install Chromium by running:sudo apt install chromium-browser chromium-codecs-ffmpeg
It usually installs Chromium in /usr/bin/ -folder. So then we change the executablePath to the below:
const browser = await puppeteer.launch({
headless: true,
executablePath: '/usr/bin/chromium-browser',
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
That’s all, running your Nodejs program will now run puppeteer successfully and should open the Chromium browser.
Enjoy!