User Agents

Web browsers and everyday web tech, explained

Run Puppeteer with Existing Chrome on macOS Guide

· 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

Running Puppeteer with an existing Chrome installation on macOS allows you to bypass the default behavior of downloading a standalone version of Chromium. This approach is particularly useful for developers who want to use their existing browser profile, leverage installed extensions, avoid redundant disk space usage, or maintain a consistent browser environment that matches their daily usage. By pointing Puppeteer to the executable path of the official Google Chrome app, you can automate a browser that is already optimized for your system and logged into necessary accounts.

What You'll Need

  • Node.js and npm: Installed on your macOS system (LTS version recommended).
  • Google Chrome: The official stable version of Chrome installed in your Applications folder.
  • A Code Editor: Visual Studio Code or any text editor of your choice.
  • Terminal Access: Basic familiarity with the macOS Terminal or iTerm2.

Step-by-Step

  1. Initialize Your Project: Open your terminal and create a new directory for your project. Run mkdir puppeteer-chrome && cd puppeteer-chrome. Initialize the project by running npm init -y to create a package.json file. This ensures your dependencies are tracked correctly.
  2. Install Puppeteer: Install the Puppeteer package using npm. Run npm install puppeteer. While Puppeteer will download a version of Chromium by default during this process, we will explicitly tell the script to ignore it and use your local Chrome installation instead.
  3. Locate Your Chrome Executable Path: On macOS, the standard installation path for Google Chrome is /Applications/Google Chrome.app/Contents/MacOS/Google Chrome. It is critical to point to the binary file inside the app bundle, not just the .app folder itself. You can verify this path by opening your terminal and typing ls "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; if the file exists, the terminal will simply return the path.
  4. Create the Automation Script: Create a file named index.js in your project folder. Use your editor to add the following code structure:
    const puppeteer = require('puppeteer');
    
    (async () => {
      const browser = await puppeteer.launch({
        executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
        headless: false, // Set to true if you don't want to see the browser window
        args: ['--no-sandbox', '--disable-setuid-sandbox']
      });
      const page = await browser.newPage();
      await page.goto('https://www.google.com');
      console.log('Page title:', await page.title());
      await browser.close();
    })();
  5. Configure User Data Directories (Optional): If you want Puppeteer to use your actual Chrome profile (bookmarks, cookies, saved passwords), you must specify the userDataDir. On macOS, this is typically found at ~/Library/Application Support/Google/Chrome. Add this line to your puppeteer.launch options: userDataDir: '/Users/[YourUsername]/Library/Application Support/Google/Chrome'. Replace [YourUsername] with your actual macOS username.
  6. Execute the Script: Run your script from the terminal using the command node index.js. If configured correctly, you will see a Google Chrome window open, navigate to the specified URL, and then close automatically.

Common Mistakes

One of the most frequent errors is pointing the executablePath to the /Applications/Google Chrome.app folder instead of the actual binary. macOS treats .app files as directories; Puppeteer requires the path to the actual executable file located deep within the Contents/MacOS/ subdirectory. If you provide the folder path, you will receive an "Error: spawn /Applications/Google Chrome.app ENOENT" or a permission denied error.

Another common mistake is attempting to launch Puppeteer using an existing Chrome profile while a physical instance of Google Chrome is already open. Chrome only allows one process to access a user profile directory at a time. If you have Chrome open for browsing and try to run a Puppeteer script that points to that same userDataDir, the script will likely crash or hang because the profile is "locked" by the active browser session.

Developers also often forget to handle asynchronous operations. Because Puppeteer is entirely Promise-based, failing to use async/await or .then() will result in the script finishing before the browser even has a chance to launch, leading to empty results or unhandled promise rejection warnings in the console.

Tips

To improve the stealth of your automation, consider using the puppeteer-extra plugin along with the puppeteer-extra-plugin-stealth. Many websites can detect that a browser is being controlled by automation software via the navigator.webdriver flag. The stealth plugin modifies the browser fingerprint to make it look like a genuine human-operated browser, which is especially effective when using your existing Chrome installation since the browser's base identity is already authentic.

When debugging, always start with headless: false. This allows you to see exactly where the script is failing, whether a popup is blocking an element, or if a page is redirecting unexpectedly. Once the logic is verified, you can switch back to headless: "new" (or true) for better performance and lower resource consumption.

If you are automating tasks that require specific window sizes, use page.setViewport({ width: 1920, height: 1080 }) immediately after creating the new page. This ensures that elements are rendered in a consistent position regardless of the default size of your installed Chrome window, which prevents "element not clickable" errors caused by responsive design changes.

Troubleshooting

Issue: "Protocol error: Target closed"
This usually happens when the browser crashes or is closed manually while the script is still trying to communicate with it. Check if your executablePath is correct and ensure that you aren't calling browser.close() too early in your code. If you are using a custom userDataDir, ensure that no other Chrome windows are open.

Issue: Permission Denied (EACCES)
This occurs when Node.js does not have the necessary permissions to execute the Chrome binary. While rare for standard installations, you can try resetting permissions via the terminal using chmod +x "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome". Additionally, ensure your terminal application (Terminal or iTerm2) has "Full Disk Access" granted in macOS System Settings > Privacy & Security.

Issue: Chrome opens but remains blank
This is often caused by a mismatch between the Puppeteer version and the Chrome version. Ensure your Puppeteer package is up to date by running npm update puppeteer. If you are using a very old version of Chrome, you may need to install a specific older version of Puppeteer that matches the Chrome DevTools Protocol version used by that browser.

Issue: Script hangs on puppeteer.launch
Check if there are zombie Chrome processes running in the background. Open Activity Monitor, search for "Google Chrome," and force quit any remaining processes that are not currently in use. These ghost processes can sometimes hold locks on the user profile or the communication port, preventing a new session from starting.

Frequently Asked Questions

Does this method slow down the automation?

No, using the existing Chrome installation is generally just as fast as using the bundled Chromium. In some cases, it can be faster if the browser is already cached with necessary assets.

Can I use this to automate my logged-in Gmail or Facebook account?

Yes, by specifying the userDataDir path to your Chrome profile, Puppeteer will launch with your existing cookies and session data, allowing you to stay logged into websites.

Will this update automatically when Chrome updates?

Yes. Since Puppeteer is pointing to the application path, it will always use whatever version of Chrome is currently installed on your Mac, regardless of updates.

Is it safe to use my main profile for automation?

It is generally safer to create a separate Chrome profile specifically for automation. This prevents the script from accidentally deleting your bookmarks or history and avoids the "profile locked" error when you want to browse the web while the script runs.

Why use Chrome instead of the default Chromium?

Google Chrome includes proprietary codecs (like H.264) and a more comprehensive set of certificates and plugins that are not present in the open-source Chromium, making it more compatible with a wider range of websites.