Skip to content

Instantly share code, notes, and snippets.

@tavinus
Created September 11, 2020 09:14
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tavinus/7effd4b3dac87cb4366e3767679a192f to your computer and use it in GitHub Desktop.
Save tavinus/7effd4b3dac87cb4366e3767679a192f to your computer and use it in GitHub Desktop.
Minimal Puppeteer NodeJS Debian 10 Buster

System

Debian 10 Buster headless

Chromium dependencies

Shared Libraries needed

$ sudo apt install libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libgbm1 libasound2 libpangocairo-1.0-0 libxss1 libgtk-3-0

Install nvm

Local user

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Reconnect/Reload or load manually

Install NodeJS

nvm install node

Test Puppeteer

Create a new foler and install puppeteer

$ mkdir test && pushd test
$ npm i puppeteer # or yarn add puppeteer

Run a test program

Create a new file

$ nano test.js

Content

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();
})();

If all went fine you will have a screenshot of the page on your current folder.

@tavinus
Copy link
Author

tavinus commented Sep 11, 2020

🚀

@bnielsen1965
Copy link

When I tried this today (2022-01-19) I ran into a couple of issues. First the browser would not launch due to a sandbox error and after resolving the sandbox issue it failed due to a missing library. The following solutions resolved the issues...

To resolve the sandbox issue I added a couple of arguments to the browser launch, use these with caution...

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    args: [
      '--no-sandbox', '--disable-setuid-sandbox'
    ]
  });
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

To resolve the missing library I added libx11-xcb1 to the list of packages to install...

sudo apt install libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libgbm1 libasound2 libpangocairo-1.0-0 libxss1 libgtk-3-0 libx11-xcb1

With these changes it worked.

@RitvikSardana
Copy link

RitvikSardana commented Jun 30, 2022

I have installed all these packages and I am still getting the errors like -

  1. browser not defined
  2. target closed
  3. Failed to launch chrome! spawn /opt/bitnami/apache/htdocs/node_modules/puppeteer/.local-chromium/linux-543305/chrome-
    linux/chrome EACCES

can you suggest me anything?

@bnielsen1965
Copy link

@RitvikSardana

What version of nodejs and debian are you running?

I tested a fresh debian and nodejs install with these versions...

debian@debian:~/test$ node --version
v16.15.1
debian@debian:~/test$ cat /etc/debian_version 
10.12
  1. I did a fresh debian install using the 10.9 net install CD. The install upgraded to debian 10.12.

  2. I did not install nvm and did not use nvm to install nodejs. I use a bash script to install a specific version of nodejs...

debian@debian:~$ wget https://github.com/bnielsen1965/nodejs-install/archive/refs/heads/master.zip
debian@debian:~$ sudo apt install unzip
debian@debian:~$ unzip master.zip 
debian@debian:~$ cd nodejs-install-master/
debian@debian:~$ sudo ./nodejs-install.sh i -v 16.15.1
  1. I then create the test directory, install puppeteer, and create the test.js script with the sandbox changes.
debian@debian:~$ npm i puppeteer
debian@debian:~$ vim test.js
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({
    args: [
      '--no-sandbox', '--disable-setuid-sandbox'
    ]
  });
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();
  1. I run the test script and it creates example.png as expected.
debian@debian:~$ node test.js

@RitvikSardana
Copy link

Node version is - 14.19.3
Debian version is - 10.12

I am unable to reslove this issue -

I get this error
EACCES: permission denied, open './test.jpg'

I am using AWS EC2 instance,

How can I solve this error?
I believe there could be some owner, group or permission error.
Can you please help

@tavinus
Copy link
Author

tavinus commented Jul 11, 2022

Can u run the test code? What code are you currently running? If you post the code with the error message, it is easier to guess. Permission errors are easy to solve, but out of scope in this case...I suggest you learn that (it is really really easy in linux/unix).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment