I can't wrap my head around the following issue. I have a development environment on Windows 11.
I try to get Spatie's Browsershot working on Windows and keep getting errors about node not being recognized as a internal or external program.
However, when I run just "node -v" from any location in a CMD-shell, I everytime see the version number. So, path vars are set right I guess. I use the latest version of Laravel and Browsershot.
When I execute this simple Tinker script through "php artisan tinker" I do see the desired html:
use Spatie\Browsershot\Browsershot;
$a = new Browsershot();
$a->url('https://www.ad.nl')->bodyHtml();
When I run this via the browser (using routes/web.php), I get errors:
Route::get('/a', function () {
$a = new Browsershot();
$a->url('https://www.ad.nl')->bodyHtml();
});
Errors:
2022-12-02 11:01:48] local.ERROR: The command "node ^"C:^\dev^\projects^\test^\vendor^\spatie^\browsershot^\src/../bin/browser.js^" ^"^{^\^"url^\^":^\^"https:^\/^\/www.ad.nl^\^",^\^"action^\^":^\^"content^\^",^\^"options^\^":^{^\^"args^\^":^[^],^\^"viewport^\^":^{^\^"width^\^":800,^\^"height^\^":600^}^}^}^"" failed.
Exit Code: 1(General error)
Working directory: C:\dev\projects\test\public
Output:
================
Error Output:
================
'node' is not recognized as an internal or external command,
operable program or batch file.
{"exception":"[object] (Symfony\\Component\\Process\\Exception\\ProcessFailedException(code: 0): The command \"node ^\"C:^\\dev^\\projects^\\test^\\vendor^\\spatie^\\browsershot^\\src/../bin/browser.js^\" ^\"^{^\\^\"url^\\^\":^\\^\"https:^\\/^\\/www.ad.nl^\\^\",^\\^\"action^\\^\":^\\^\"content^\\^\",^\\^\"options^\\^\":^{^\\^\"args^\\^\":^[^],^\\^\"viewport^\\^\":^{^\\^\"width^\\^\":800,^\\^\"height^\\^\":600^}^}^}^\"\" failed.
Exit Code: 1(General error)
Working directory: C:\\dev\\projects\\test\\public
Output:
================
Error Output:
================
'node' is not recognized as an internal or external command,
operable program or batch file.
Does anybody got a clue where the problem might be?
For problem "'node' is not recognized as an internal or external command", you can set node path explicitly.
https://spatie.be/docs/browsershot/v2/requirements#content-custom-node-and-npm-binaries
Example:
Browsershot::url('https://google.com')->setNodeBinary('PATH %~dp0;%PATH%;')->setNpmBinary('PATH %~dp0;%PATH%;');
Make sure your node register on your PATH.