Error using Spatie/Browsershot in Laravale : npm & node not found

1k Views Asked by At

I installed Spatie/Browsershot with composer and immediately got an error which is above my pay-grade... can someone please help.

 use Spatie\Browsershot\Browsershot;

 Browsershot::html('testing')->save('/public/assets/pdfs/example.pdf');

 The command "PATH=$PATH:/usr/local/bin:/opt/homebrew/bin NODE_PATH=`npm root -g` node '/var/www/utopia/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"file:\/\/\/tmp\/112376137-0051711001687420484\/index.html","action":"pdf","options":{"path":"\/public\/assets\/pdfs\/example.pdf","args":[],"viewport":{"width":800,"height":600},"displayHeaderFooter":false}}'" failed. Exit Code: 127(Command not found) Working directory: /var/www/utopia Output: ================ Error Output: ================ sh: 1: npm: not found sh: 1: node: not found 

 Browsershot::html('testing')->save('/public/assets/pdfs/example.pdf')->setNodeBinary('/usr/bin/node')->setNpmBinary('/usr/bin/npm');

Same error.

I am using Docker. Is that the problem? What should I do?

As suugested by lance2k, here is the list of paths I get when running echo $PATH inside my laradock project

 cd laradock
 docker-compose exec workspace bash
 /var/www# cd project
 root@e71af771fcba:/var/www/project# echo $PATH
 /home/laradock/.yarn/bin
 /home/laradock/.nvm/versions/node/v20.0.0/bin
 /root/.composer/vendor/bin
 /usr/local/sbin
 /usr/local/bin
 /usr/sbin
 /usr/bin
 /sbin
 /bin
 /var/www/vendor/bin

Which one should I use to tell BrowserShot where is npn?

So now I am using :

 Browsershot::html('testing')->save('/public/assets/temp/example.pdf')->noSandbox()->setIncludePath('/home/laradock/.nvm/versions/node/v20.0.0/bin');

And I get the following error:

 The command "PATH=$PATH:/usr/local/bin:/opt/homebrew/bin NODE_PATH=`npm root -g` node '/var/www/travelearn/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"file:\/\/\/tmp\/637242465-0323651001694335282\/index.html","action":"pdf","options":{"path":"\/public\/assets\/temp\/example.pdf","args":[],"viewport":{"width":800,"height":600},"displayHeaderFooter":false}}'" failed. Exit Code: 127(Command not found) Working directory: /var/www/travelearn/public Output: ================ Error Output: ================ sh: 1: npm: not found sh: 1: node: not found 

As I am still getting the error after use setBinary... I decided to re-install pupeteer but it seems that there are many issues... please help.

 npm install puppeteer
 npm WARN deprecated [email protected]: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compatibility
 npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
 npm WARN deprecated [email protected]: This SVGO version is no longer supported. Upgrade to v2.x.x.
 npm WARN deprecated [email protected]: You can find the new Popper v2 at @popperjs/core, this package is dedicated to the legacy v1
 npm WARN deprecated [email protected]: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.
 
 added 1274 packages, and audited 1275 packages in 2m
 
 142 packages are looking for funding
   run `npm fund` for details
 
 14 vulnerabilities (6 moderate, 8 high)
 
 To address issues that do not require attention, run:
   npm audit fix
 
 Some issues need review, and may require choosing
 a different dependency.
 
 Run `npm audit` for details.
 npm notice 
 npm notice New major version of npm available! 9.6.4 -> 10.1.0
 npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.1.0
 npm notice Run npm install -g [email protected] to update!
 npm notice 
 root@e71af771fcba:/var/www/travelearn# which node
 /home/laradock/.nvm/versions/node/v20.0.0/bin/node
 root@e71af771fcba:/var/www/travelearn# npm audit
 # npm audit report
 
 nth-check  <2.0.1
 Severity: high
 Inefficient Regular Expression Complexity in nth-check - https://github.com/advisories/GHSA-rp65-9cf3-cjxr
 No fix available
 node_modules/css-select/node_modules/nth-check
   css-select  <=3.1.0
   Depends on vulnerable versions of nth-check
   node_modules/css-select
     svgo  1.0.0 - 1.3.2
     Depends on vulnerable versions of css-select
     node_modules/svgo
       postcss-svgo  4.0.0-nightly.2020.1.9 - 5.0.0-rc.2
       Depends on vulnerable versions of svgo
       node_modules/postcss-svgo
         cssnano-preset-default  <=4.0.8
         Depends on vulnerable versions of postcss-svgo
         node_modules/cssnano-preset-default
           cssnano  4.0.0-nightly.2020.1.9 - 4.1.11
           Depends on vulnerable versions of cssnano-preset-default
           node_modules/cssnano
             webpack-rtl-plugin  *
             Depends on vulnerable versions of @romainberger/css-diff
             Depends on vulnerable versions of cssnano
             Depends on vulnerable versions of rtlcss
             node_modules/webpack-rtl-plugin
 
 postcss  <7.0.36
 Severity: moderate
 Regular Expression Denial of Service in postcss - https://github.com/advisories/GHSA-566m-qj78-rww5
 No fix available
 node_modules/@romainberger/css-diff/node_modules/postcss
 node_modules/webpack-rtl-plugin/node_modules/postcss
   @romainberger/css-diff  *
   Depends on vulnerable versions of postcss
   node_modules/@romainberger/css-diff
   rtlcss  <=2.6.2
   Depends on vulnerable versions of postcss
   node_modules/webpack-rtl-plugin/node_modules/rtlcss
 
 quill  <=1.3.7
 Severity: moderate
 Cross-site Scripting in quill - https://github.com/advisories/GHSA-4943-9vgg-gr5r
 No fix available
 node_modules/quill
 
 semver  6.0.0 - 6.3.0 || 7.0.0 - 7.5.1
 Severity: moderate
 semver vulnerable to Regular Expression Denial of Service - https://github.com/advisories/GHSA-c2qf-rxjj-qqgw
 semver vulnerable to Regular Expression Denial of Service - https://github.com/advisories/GHSA-c2qf-rxjj-qqgw
 fix available via `npm audit fix`
 node_modules/core-js-compat/node_modules/semver
 node_modules/css-loader/node_modules/semver
 node_modules/laravel-mix/node_modules/semver
 node_modules/node-notifier/node_modules/semver
 node_modules/postcss-loader/node_modules/semver
 node_modules/semver
   core-js-compat  3.6.0 - 3.25.0
   Depends on vulnerable versions of semver
   node_modules/core-js-compat
 
 webpack  5.0.0 - 5.75.0
 Severity: high
 Cross-realm object access in Webpack 5 - https://github.com/advisories/GHSA-hc6q-2mpp-qw7j
 fix available via `npm audit fix`
 node_modules/webpack
 
 14 vulnerabilities (6 moderate, 8 high)
 
 To address issues that do not require attention, run:
   npm audit fix
 
 Some issues need review, and may require choosing
 a different dependency.
 
1

There are 1 best solutions below

10
On

If you installed node using Node Version Manager, you have to modify the include path, you can set it using the setIncludePath method.

Browsershot::html('Foo')
    ->setIncludePath('$PATH:/usr/local/bin')

Use which node and look for the path for your node.

lance@lance-vivobook:~$ which node
/usr/bin/node

Then use ->setIncludePath('$PATH:/usr/bin')

Make sure path is included on your system path:

lance@lance-vivobook:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/home/lance/.config/composer/vendor/bin

Setting the include path can be useful in cases where node and npm can not be found automatically.