User Agents

Web browsers and everyday web tech, explained

Install Puppeteer Node.js and Chrome: Setup & Troubleshooting

· 6 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Installing Puppeteer allows you to control a headless Chrome or Chromium browser via Node.js, enabling you to automate tasks like generating PDFs, scraping single-page applications, and automated UI testing. By installing the puppeteer package, you gain a high-level API that communicates with the browser through the DevTools Protocol, allowing for precise manipulation of the DOM and browser state without requiring a visible window.

What You'll Need

  • Node.js: An LTS (Long Term Support) version is highly recommended to ensure compatibility with the latest Puppeteer releases.
  • npm or yarn: A package manager to handle the installation of the library.
  • Terminal/Command Prompt: Access to a shell environment (Bash, Zsh, or PowerShell) with administrative permissions to install system dependencies.
  • Internet Connection: Required to download the Puppeteer library and the compatible version of Chromium.

Step-by-Step

  1. Initialize Your Project: Create a new directory for your project and navigate into it using your terminal. Run npm init -y to generate a package.json file. This file tracks your dependencies and ensures that other developers can replicate your environment.
  2. Install Puppeteer: Run the command npm install puppeteer. It is important to note that there are two versions of the library. The standard puppeteer package automatically downloads a compatible version of Chromium into your node_modules folder. If you prefer to use an existing Chrome installation on your system, you can install puppeteer-core instead, though this requires manual path configuration.
  3. Verify the Installation: Create a file named index.js in your project root. To ensure the installation worked, write a simple script that launches the browser, takes a screenshot of a website, and closes. Use the following logic: import puppeteer, call puppeteer.launch(), use browser.newPage(), navigate to a URL using page.goto(), and finally call browser.close().
  4. Execute the Script: Run your script using the command node index.js. If the process completes without errors and generates the expected screenshot file, your installation is successful.
  5. Configure Headless Mode: By default, Puppeteer runs in "headless" mode, meaning no browser window appears. If you need to see what is happening for debugging purposes, modify the launch options to puppeteer.launch({ headless: false }). This will open a visible Chrome window during execution.
  6. Install System Dependencies (Linux Only): If you are on Ubuntu or Debian, Chromium may fail to launch due to missing shared libraries. You must install these dependencies via the terminal. Common missing libraries include libx11-xcb1, libxcomposite1, and libatk1.0-0. Using sudo apt-get install with the full list of required libraries is essential for server-side deployments.

Common Mistakes

One of the most frequent errors is confusing puppeteer with puppeteer-core. Users often install puppeteer-core and then wonder why the browser fails to launch. Remember that puppeteer-core does not come with a bundled browser; you must provide the executablePath pointing to a valid Chrome binary on your machine. If you are a beginner, stick to the standard puppeteer package.

Another common mistake is neglecting the await keyword. Puppeteer is almost entirely asynchronous. Every interaction—launching the browser, opening a page, clicking a button, or waiting for a selector—returns a promise. Forgetting a single await can lead to "race conditions" where the script attempts to click a button before the page has even loaded, resulting in the dreaded Execution context was destroyed error.

Many developers also forget to close the browser instance. If your script crashes or finishes without calling await browser.close(), you will leave "zombie" Chrome processes running in the background. Over time, these processes consume all available RAM, slowing down your entire system and eventually causing subsequent Puppeteer scripts to fail because the system cannot allocate more memory.

Tips

To speed up your scraping or automation, disable unnecessary features. For example, you can block images and CSS from loading by using page.setRequestInterception(true) and checking the request type. This significantly reduces bandwidth and increases page load speeds when you only need the raw HTML data.

When dealing with modern websites that use heavy JavaScript, avoid using fixed timers like setTimeout. Instead, use page.waitForSelector() or page.waitForNavigation(). This ensures that your script only proceeds once the specific element you need is actually present in the DOM, making your code more robust across different network speeds.

For those deploying to Docker, use a specialized base image or a custom Dockerfile that includes the necessary Chromium dependencies. The official Puppeteer documentation provides a recommended set of apt-get commands to ensure the environment is fully compatible. Always run the browser with the --no-sandbox flag in Docker environments to avoid permission issues related to the Linux kernel's sandboxing features.

Troubleshooting

Error: "Failed to launch the browser process"
This is the most common error on Linux servers. It almost always indicates missing system libraries. Check the terminal output for specific missing .so files. You can find the full list of dependencies for Chromium on the Puppeteer GitHub repository. Install the missing packages using your system's package manager. If you are on Windows, ensure that your antivirus or firewall is not blocking the Chromium executable located inside the node_modules folder.

Error: "Protocol error (Runtime.callFunctionOn): Execution context was destroyed"
This occurs when the page navigates to a new URL or refreshes while Puppeteer is still trying to execute a command on the previous page state. To fix this, ensure you are awaiting the navigation completely. Use await Promise.all([page.waitForNavigation(), page.click('selector')]) when a click triggers a page load. This ensures the script waits for the new page to stabilize before continuing.

Browser hangs or freezes
If your script hangs indefinitely, it is likely waiting for a selector that never appears. Implement a timeout using the timeout option in waitForSelector({ timeout: 5000 }). This prevents the script from running forever and allows you to catch the error and handle it gracefully, perhaps by refreshing the page or logging a failure.

Memory Leaks and High CPU Usage
If you are running a long-term automation task, do not use a single browser instance for thousands of pages. Browsers naturally leak memory over time. The best practice is to launch a browser, open a few pages (tabs), perform the tasks, and then close the entire browser instance every 50-100 pages to clear the memory cache.

Frequently Asked Questions

Does Puppeteer require Google Chrome to be installed?

No, the standard puppeteer package downloads its own compatible version of Chromium automatically. However, you can configure it to use your existing Chrome installation if you prefer.

What is the difference between Headless and Headful mode?

Headless mode runs the browser in the background without a GUI, which is faster and ideal for servers. Headful mode (headless: false) opens a visible window, which is essential for debugging and seeing how the page renders.

Can Puppeteer bypass CAPTCHAs?

Puppeteer cannot natively solve CAPTCHAs as they are designed to block automation. You would need to integrate third-party CAPTCHA solving services via their APIs to handle these challenges.

Is Puppeteer better than Selenium?

Puppeteer is generally faster and easier to set up for Chrome/Chromium automation because it uses the DevTools Protocol. Selenium is better if you need to test across multiple different browsers like Firefox, Safari, or Edge.

How do I handle pop-ups or alerts in Puppeteer?

You can use page.on('dialog', async dialog => { await dialog.dismiss(); }) to automatically handle and close alerts, confirms, or prompts that appear during the session.