Fix: "ReferenceError: executablePath is not defined" in Puppeteer

October 31, 2022

Getting error when trying to run your Google Puppeteer script which is based on puppeteer-extra or puppeteer-core ?

executablePath is not defined error

Well, it turns out we need to set the executablePath manually since those packages does not comes with executable out of box.

There are two ways in which we can fix this issue.

Approach #1: Use your browser’s executablePath

If you’re using chromium based browsers like Chrome/Brave/Edge/Opera we can use it’s executablePath for our puppeteer script instead of downloading it seperately.

  // Windows
  const executablePath = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe';

  // If your're on Mac
  const executablePath = '/Applications/Google Chrome.app/Contents/MacOS/Google\ Chrome'

  const browser = await puppeteer.launch({
          executablePath,
  });

Approach #2: Use puppeteer’s executablePath

Unlike the above approach, we’ll be using puppeteer package which by default downloads a recent version of Chromium.

  npm install puppeteer
  const { executablePath } = require('puppeteer')
  const browser = await puppeteer.launch({
          executablePath: executablePath(),
  });

Reference


👋 I'm Ashik Nesin • Principal Software Engineer @Chargebee 👨‍💻• I write about web development & behinds the scenes of startup/side projects that I'm building

© 2022 Nesin Technologies LLP