Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bun add puppeteer doesn't run postinstall script for trusted dependency #4705

Closed
dr-noid opened this issue Sep 9, 2023 · 23 comments
Closed
Labels
bug Something isn't working

Comments

@dr-noid
Copy link

dr-noid commented Sep 9, 2023

What version of Bun is running?

1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983

What platform is your computer?

Linux 5.15.90.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

mkdir bun-puppeteer
cd bun-puppeteer
bun init
add "trustedDependencies": ["puppeteer"] to package.json
package.json reference:

{
  "name": "bun-puppeteer",
  "module": "index.ts",
  "type": "module",
  "devDependencies": {
    "bun-types": "latest"
  },
  "peerDependencies": {
    "typescript": "^5.0.0"
  },
  "trustedDependencies": [
    "puppeteer"
  ]
}

bun add puppeteer
bun install

index.ts:

import puppeteer from "puppeteer";
await puppeteer.launch({ headless: false });

bun run index.ts

What is the expected behavior?

bun run starts a headless browser

What do you see instead?

bun-puppeteer $ bun run index.ts

269 | if (this.puppeteer.configuration.browserRevision) {
270 | throw new Error(Tried to find the browser at the configured path (${executablePath}) for revision ${this.puppeteer.browserRevision}, but no executable was found.);
271 | }
272 | switch (this.product) {
273 | case 'chrome':
274 | throw new Error(Could not find Chrome (ver. ${this.puppeteer.browserRevision}). This can occur if either\n +
^
error: Could not find Chrome (ver. 116.0.5845.96). This can occur if either

  1. you did not perform an installation before running the script (e.g. npm install) or
  2. your cache path is incorrectly configured (which is: /home/drnoid/.cache/puppeteer).
    For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
    at resolveExecutablePath (/home/drnoid/coding/bun-puppeteer/node_modules/puppeteer-core/lib/esm/puppeteer/node/ProductLauncher.js:274:26)
    at /home/drnoid/coding/bun-puppeteer/node_modules/puppeteer-core/lib/esm/puppeteer/node/ChromeLauncher.js:92:31
    at processTicksAndRejections (:1:2602)

Additional information

There is a 99.9% chance that this is an issue with the postinstall script of puppeteer.
If I install puppeteer with pnpm or npm it will run the postinstall script and download the browser in the .cache directory.

@dr-noid dr-noid added the bug Something isn't working label Sep 9, 2023
@dr-noid
Copy link
Author

dr-noid commented Sep 9, 2023

I found a little workaround for now:

in my package.json I added a postinstall script with
bunx @puppeteer/browsers install chrome@stable --path $HOME/.cache/puppeteer

using the puppeteer CLI to install the missing chrome driver.

@geongeorge
Copy link

@dr-noid I feel you are right, Chrome was not downloaded when I installed puppeteer with bun.

@stewones
Copy link

stewones commented Sep 10, 2023

same here. postinstall ^ worked though 🙏

@Alino
Copy link

Alino commented Sep 11, 2023

I am on m1 pro mac and adding the postinstall script did run the post-install command after running bun install. However it had no effect and the issue remains.

bun install v1.0.0 (822a00c4)
[email protected] /Users/admin/.cache/puppeteer/chrome/mac_arm-116.0.5845.96/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing

bun start
[2023-09-11T14:18:53.314Z]  Could not find Chrome (ver. 115.0.5790.98). This can occur if either
 1. you did not perform an installation before running the script (e.g. `npm install`) or
 2. your cache path is incorrectly configured (which is: /Users/admin/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
/Users/admin/Documents/Projects/blabla/node_modules/puppeteer-core/src/node/ProductLauncher.ts:438
          throw new Error(
                ^
Error: Could not find Chrome (ver. 115.0.5790.98). This can occur if either
 1. you did not perform an installation before running the script (e.g. `npm install`) or
 2. your cache path is incorrectly configured (which is: /Users/admin/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.

UPDATE: I fixed it by using the specific chrome version to install in the postinstall script.
"postinstall": "bunx @puppeteer/browsers install [email protected] --path $HOME/.cache/puppeteer"

@dr-noid
Copy link
Author

dr-noid commented Sep 11, 2023

@Alino glad you found a fix, this issue probably won't get any traction because from the outside it looks like an issue with puppeteer. I haven't done any testing on this but my guess is this is an issue with trustedDependencies and postinstall scripts as a whole.

@rgl
Copy link

rgl commented Sep 11, 2023

It's a two fold problem.

  1. Puppeteer postinstall calls the node cli.
  2. Installing the browser and immediately running is broken in bun.

See #4894

@Parzival-3141
Copy link
Contributor

Parzival-3141 commented Sep 11, 2023

Related #4959

@azat-io
Copy link

azat-io commented Sep 11, 2023

bunx @puppeteer/browsers install chrome@stable --path $HOME/.cache/puppeteer not works for me. I'm trying to do it in docker

@dr-noid
Copy link
Author

dr-noid commented Sep 12, 2023

@azat-io i don't have much experience with docker so I don't know how the filesystem structure is laid out, you could always omit the --path argument, in that case puppeteer will install it under ${cwd}/chrome and you can reference this path when using puppeteer.launch({}) by argument.

@An-GG
Copy link

An-GG commented Sep 14, 2023

If I install puppeteer with pnpm or npm it will run the postinstall script and download the browser in the .cache directory.

So I haven't been able to reproduce the issue using your procedure (the first install of puppeteer on a system using bun in a brand new project has consistently worked for me so far) but I've experienced a lot of similar issues on subsequent runs of bun install.

I think the problem basically stems from bun only calling postinstall scripts when you first add a package (aka when it first gets added to bun.lockb), whereas npm will run a package's postinstall scripts every time it is added to the node_modules directory. Critically, you commit your bun lockfile to git, but obviously you don't commit node_modules.

If the files created by puppeteer's postinstall script (in ~/.cache/puppeteer) are damaged or removed, they'll stay that way because bun will basically never call the postinstall script again (unless you delete bun.lockb, which works but obv I'd like to keep dep versions locked).

npm faces a similar issue actually. If you do an npm install, confirm puppeteer is working, and then delete ~/.cache/puppeteer without changing the node_modules dir, npm will not rerun postinstall scripts and you'll get the same error. This is quickly fixed if you reclone your project and npm install again, but doing the same for bun does not work (bc bun.lockb).

@dr-noid
Copy link
Author

dr-noid commented Sep 14, 2023

@An-GG thanks for your response, I actually tried to look more into it after you mentioned the bun.lockb file and I think I exactly when the issue happens.

You can recreate the problem like this:

  1. Init project with bun init
  2. Add puppeteer to trustedDependencies
  3. Install puppeteer bun add, bun install etc <-- postinstall not ran
    image

However, it does run the postinstall script if I do this:

  1. Init project with bun init
  2. Add puppeteer to trustedDependencies
  3. Delete the auto-generated Bun lockfile rm bun.lockb
  4. Install puppeteer bun add (without a bun.lockb present, postinstall is ran)
    image

@An-GG
Copy link

An-GG commented Sep 14, 2023

@dr-noid That's really interesting - so even just an empty bun.lockb prevented the postinstall hook from running.
But then once it's in bun.lockb, running bun install in a fresh clone of your project does properly run the postinstall hook???

I had assumed this was a result of too eagerly reusing cached installs of packages, but it seems like its a much more glaring bug.

Personally I am still confused on the proper usage of bun add vs bun install. Half the time in my testing I accidentally used bun install puppeteer out of habit and that seems to do the same thing as bun add, I think, but the docs don't discuss using install that way so I'm feeling like I'm in undefined behavior territory.

I have no evidence of this but specifically regarding the puppeteer postinstall issue, I think at one point I encountered better success with using install puppeteer instead of add puppeteer. This may have been entirely hallucinated.

@dr-noid
Copy link
Author

dr-noid commented Sep 14, 2023

@An-GG I have only been able to get Bun to run the postinstall hook when I did bun add puppeteer WITHOUT the lockfile present.

@scsmash3r
Copy link

How do you guys manage to run it at all? All I'm getting at any case, when trying to install a browsers, is:

sc@SC-FVF:~/www/buntest$ bunx @puppeteer/browsers install [email protected] --path $HOME/.cache/puppeteer
[0.01ms] ".env"
92 |         if (required) {
93 |             yargs.demandOption('path');
94 |         }
95 |     }
96 |     async run(argv) {
97 |         const yargsInstance = (0, yargs_1.default)((0, helpers_1.hideBin)(argv));
                  ^
TypeError: (0, yargs_1.default) is not a function. (In '(0, yargs_1.default)((0, helpers_1.hideBin)(argv))', '(0, yargs_1.default)' is "/home/sc/www/buntest/node_modules/@puppeteer/browsers/node_modules/yargs/yargs")   
      at /home/sc/www/buntest/node_modules/@puppeteer/browsers/lib/cjs/CLI.js:97:14
      at run (/home/sc/www/buntest/node_modules/@puppeteer/browsers/lib/cjs/CLI.js:96:14)
      at /home/sc/www/buntest/node_modules/@puppeteer/browsers/lib/cjs/main-cli.js:20:5
      at globalThis (/home/sc/www/buntest/node_modules/@puppeteer/browsers/lib/cjs/main-cli.js:20:40)        
error: "browsers" exited with code 1 (SIGHUP)

Puppeteer installed is 21.2.1...

@An-GG
Copy link

An-GG commented Sep 14, 2023

@dr-noid
oh okyeah that's consistent

Adding typescript, then puppeteer doesn't work

an@fast:~/Projects/PackageD$ bun add typescript
bun add v1.0.1 (31aec4eb)

 installed [email protected] with binaries:
  - tsc
  - tsserver


 1 packages installed [846.00ms]
an@fast:~/Projects/PackageD$ bun add puppeteer
bun add v1.0.1 (31aec4eb)

 installed [email protected]


 109 packages installed [769.00ms]

But adding puppeteer, then typescript works

an@fast:~/Projects/PackageD$ bun remove puppeteer
bun remove v1.0.1 (31aec4eb)
 - puppeteer

 1 packages removed [2.00ms]
an@fast:~/Projects/PackageD$ bun remove typescript
bun remove v1.0.1 (31aec4eb)
[2.00ms] done
No packages! Deleted empty lockfile
an@fast:~/Projects/PackageD$ bun add puppeteer
bun add v1.0.1 (31aec4eb)

 installed [email protected]

Downloading chrome r116.0.5845.96 - 145.9 MB [====================] 100% 0.0s 
Chrome (116.0.5845.96) downloaded to /home/an/.cache/puppeteer/chrome/linux-116.0.5845.96

 1 packages installed [4.00ms]
an@fast:~/Projects/PackageD$ bun add typescript
bun add v1.0.1 (31aec4eb)

 installed [email protected] with binaries:
  - tsc
  - tsserver


 1 packages installed [13.00ms]

crazy

what a weird bug

@scsmash3r
Copy link

I found a little workaround for now:

in my package.json I added a postinstall script with bunx @puppeteer/browsers install chrome@stable --path $HOME/.cache/puppeteer

using the puppeteer CLI to install the missing chrome driver.

Not working for me under Ubuntu 22.04.2 LTS, getting this output:

❯ bunx @puppeteer/browsers install [email protected] --path $HOME/.cache/puppeteer
/var/www/buntests/node_modules/@puppeteer/browsers/lib/cjs/CLI.js:65
    #defineBrowserParameter(yargs) {
                           ^

SyntaxError: Unexpected token '('
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/var/www/buntests/node_modules/@puppeteer/browsers/lib/cjs/main-cli.js:19:18)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
error: "browsers" exited with code 1 (SIGHUP)

@An-GG
Copy link

An-GG commented Sep 14, 2023

Not working for me under Ubuntu 22.04.2 LTS, getting this output:

alternative solution: bun add should throw an error when it encounters a postinstall script instead of not running the postinstall script

@nigelnindodev
Copy link

I've been holding off switching from Node to Bun due to this exact issue with Puppeteer, and it's the first thing I see when looking at issues with Bun/Puppeteer.

Definitely not a Bun issue but an issue with Puppeteer.

@dr-noid Since you launched Puppeteer with headless set to false, Puppeteer was not defaulting to using your existing Chrome browser?

This would be the expected behaviour or you were running on your local machine.
But wouldn't work on a remote server. Curious about that.

Very similar to some of the issues I ran into when setting up Puppeteer with Docker. I'll take a look at this, looks like the perfect issue to help resolve and also switch to Bun!

@dr-noid
Copy link
Author

dr-noid commented Sep 18, 2023

@nigelnindodev I do all of my programming in WSL so no it doesn't.

@taranek
Copy link

taranek commented Sep 19, 2023

In my case (using puppeteer for jsdom-screenshot library) I did the following as a workaround for this issue:

  1. install Chrome executable via:
    bunx @puppeteer/browsers install chrome@stable --path $HOME/.cache/puppeteer
    which prompted:
Downloading chrome r117.0.5938.88 - 126.8 MB [====================] 100% 0.0s 
[email protected]  ("executable path that needs to be copied to .env file" ->) /Users/<MY_USER_NAME>/.cache/puppeteer/chrome/mac_arm-117.0.5938.88/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing
  1. Set the following env variable in the .env file:
PUPPETEER_EXECUTABLE_PATH=/Users/<MY_USER_NAME>/.cache/puppeteer/chrome/mac_arm-117.0.5938.88/chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing
  1. Run puppeteer again

alifeee added a commit to alifeee/json-resume that referenced this issue Oct 13, 2023
alifeee added a commit to alifeee/json-resume that referenced this issue Oct 13, 2023
alifeee added a commit to alifeee/json-resume that referenced this issue Oct 13, 2023
alifeee added a commit to alifeee/json-resume that referenced this issue Oct 13, 2023
@Electroid
Copy link
Contributor

Duplicate of #4959 (we fill fix this)

@Electroid Electroid closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2023
@alifeee
Copy link

alifeee commented Oct 29, 2023

even after trying the fixes here, I couldn't get it to work.

#5416 worked for me.

(running on WSL Ubuntu)

@Jarred-Sumner
Copy link
Collaborator

This was fixed, we run puppeteer in CI so it should be pretty good now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests