Fix “running as root without –no-sandbox” Error in Puppeteer

Updated
Puppeteer

Puppeteer is a powerful tool for browser automation and scraping provided by the Google Dev Team. Built as a node library, Puppeteer runs a headless version of Chrome or Chromium and can perform many tasks. From taking screenshots to automating form submissions. When users first get started with Puppeteer, you might encounter a standard error.

Failed to launch the browser process! 
Running as root without --no-sandbox is not supported.
      
    

Let’s take a look at how to fix “running as root without –no-sandbox” Error in Puppeteer.

The Solution

The solution is straightforward but comes with some caveats. To fix the error outright, we can add some flags to the Puppeteer browser launcher.

 puppetter.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']})
      
    

This command adds two flags to our launcher –no-sandbox and –disable-setuid-sandbox.

Although this will make your puppeteer code run, the developers discourage users from adding these flags as they can pose a security risk. Only use the above two flags if you trust the content you will be opening in the headless Chrome browser using Puppeteer. 

Considering adding a user to your environment and enabling user namespace cloning. Check out more info on the subject here

Now you know how to fix “running as root without –no-sandbox” error in Puppeteer. We hope you have fun playing with Puppeteer. Happy Coding!