From e6a6295d9a7480bb59ee58a2cc7785171fa0fa2c Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Tue, 22 Feb 2022 11:22:25 +0100 Subject: [PATCH] fix: use both __dirname and require.resolve to support different bundlers (#8046) Issues: #8044 --- src/initialize-node.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/initialize-node.ts b/src/initialize-node.ts index b3170031..3d38a470 100644 --- a/src/initialize-node.ts +++ b/src/initialize-node.ts @@ -20,10 +20,21 @@ import { sync } from 'pkg-dir'; import { dirname } from 'path'; import { Product } from './common/Product.js'; +function resolvePuppeteerRootDirectory(): string | undefined { + try { + // In some environments, like esbuild, this will throw an error. + // We suppress the error since the bundled binary is not expected + // to be used or installed in this case and, therefore, the + // root directory does not have to be known. + return sync(dirname(require.resolve('./initialize-node'))); + } catch (error) { + // Fallback to __dirname. + return sync(__dirname); + } +} + export const initializePuppeteerNode = (packageName: string): PuppeteerNode => { - const puppeteerRootDirectory = sync( - dirname(require.resolve('./initialize-node')) - ); + const puppeteerRootDirectory = resolvePuppeteerRootDirectory(); let preferredRevision = PUPPETEER_REVISIONS.chromium; const isPuppeteerCore = packageName === 'puppeteer-core'; // puppeteer-core ignores environment variables