Browsershot module not found browser.js

255 views Asked by At

I've been using spatie/browsershot for some time without issue. At some point recently I must've updated the packages or something changed and now it's not working once deployed to Forge. I've followed the scripts provided by the Browsershot documentation that are specifically related to Forge:

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs gconf-service libasound2 ...(leaving out add'l for brevity)
sudo npm install --location=global --unsafe-perm puppeteer
sudo chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium

slightly modified due to 1) getting an error in the ubuntu environment regarding node 14 being too old, and 2) the change to the puppeteer cache location that happened as of v19.

For the first issue, I replaced the first line above with what was listed in the instructions I got from the upgrade docs with Forge.

For the second issue, I added

chmod -R o+rx ~/.cache/puppeteer

Also, .local-chromium doesn't exist, so I took this line out:

sudo chmod -R o+rx /usr/lib/node_modules/puppeteer/.local-chromium

I don't know why that line isn't working, I did run the install.mjs file located in usr/lib/node_modules/puppeteer in hopes that manually running the install script would fix it. I had read somewhere that there's some issue in puppeteer that causes the install script to not run in certain environments:

 node node_modules/puppeteer/install.mjs
Downloading chrome r118.0.5993.70 - 147.8 MB [====================] 100% 0.0s
Chrome (118.0.5993.70) downloaded to /home/forge/.cache/puppeteer/chrome/linux-118.0.5993.70

That doesn't seem to make any difference whatsoever.

When I deployed to Forge, I also got the error on the chmod command that the folder didn't exist at /home/forge/.cache/puppeteer, so I went ahead and created it.

I also added a .puppeteerrc.cjs file, removed and reinstalled puppeteer for that to take effect.

After doing the above, I'm getting this error, which someone else also encountered on laravel forge (https://github.com/spatie/browsershot/discussions/769), but there's no resolution:

node:internal/modules/cjs/loader:959
 throw err;
 ^
Error: Cannot find module '/home/forge/my-app.com/vendor/spatie/browsershot/bin/browser.js'
   at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
   at Function.Module._load (node:internal/modules/cjs/loader:804:27)
   at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
   at node:internal/main/run_main_module:17:47 {
 code: 'MODULE_NOT_FOUND',
 requireStack: []
}

If I navigate to the vendor/spatie/browsershot/bin directory, there is no browser.js file. There's a browser.cjs file, but nothing else.

I tried to dig into the Browsershot source code to try and determine how this command is generated, but I can't seem to figure out what's going on.

Has anyone encountered the cannot find module browser.js error before?

UPDATE

As a last ditch effort, I renamed the browser.cjs file to browser.js and now it's working fine. It's definitely a hack, and something I'll have to remember before updating browsershot in the future, but at least it's working.

1

There are 1 answers

0
hyphen On

As a last ditch effort, I renamed the browser.cjs file to browser.js and now it's working fine. It's definitely a hack, and something I'll have to remember before updating browsershot in the future, but at least it's working.