0
votes

I'm trying to configure Spatie/Browsershot for a project, written in Laravel but after completing all of the steps, I still get one error:

'node' is not recognized as an internal or external command, operable program or batch file

A have installed the latest versions of node and npm and the both of them exist in the PATH and work on cmd as well.

Like it's written in the official documentation on GitHub, I've ran:

  • npm install puppeteer
  • composer require spatie/browsershot

In the code I've written:

use Spatie\Browsershot\Browsershot;

//Method to generate some random id
$unique_id = uniqid('img_');

//The path to the node and npm folders
$path_node = realpath('C:\\"Program Files"\\nodejs');
$path_npm = realpath('C:\\Users\\Hristo\\AppData\\Roaming\\npm');


//The $content is actually a stored HTML code        
Browsershot::html("$content")->setScreenshotType('jpeg', 100)
                             ->setNodeBinary($path_node)
                             ->setNpmBinary($path_npm)
                              ->save("$unique_id.jpeg");

Program Files is with double quotation marks, otherwise Laravel throws me an error because of the white space in between the two words.

I'm not sure about the paths, are they correctly written? (the problem with the backslashes in windows)

2
Shouldn't ->setXyzBinary() take the executables name, rather than just the directory?mario
Try $node_path = '"C:\Program Files\nodejs\node.exe"';? (and the same for npm)Travis Britz
@TravisBritz it's throws: '"C:\Program' is not recognized as an internal or external command, operable program or batch file.H.Karatsanov
@mario I didn't see this function in the documentation.H.Karatsanov
@H.Karatsanov was that thrown at the node line, or did you forget to update the $path_npm at the same time as $path_node? Also, mario used Xyz to represet setNodeBinary() and setNpmBinary()Travis Britz

2 Answers

0
votes

If you can run node -v and get the version of your node. Then, the error was caused by the space between "Program Files". To solve this:

  1. Create a new folder in your local disk (C:), i.e. Programs
  2. Move your the Node folder to the folder you just created.
  3. Change the Node "Path" in the "User Variable" inside the "Environment Variable" i.e. C:\Programs\nodejs. NOTE: ONLY THE DIRECTORY CONTAINING THE .exe FILE IS NEEDED NOT THE FILE.
  4. You can then use the path for the "setNodeBinary" i.e. "C:\Programs\\nodejs\\node.exe". NOTE: THE .exe FILE IS REQUIRED HERE

This should be able to solve the issue.

0
votes

This little snippet helped me with the problem. As long as OS environmental variables are pointing to the node.exe file you can put this into your code

->setNodeBinary('PATH %~dp0;%PATH%;') //~dp0 : fetches current directory path in windows

Hope you get it solved