nillegal's avatar

Node and Npm from Nvm not found when using Browsershot

Hello everyone, So I am actually using Spatie's Laravel Pdf package, I have followed everything in the docs especially about the requirements. I have Node and Npm installed from Nvm.

When I try :

      Browsershot::html('Foo')
        ->save('invoice.pdf');

It returns the Node and Npm error saying they are not found. So I tried both Binary method and Include path method :

    Browsershot::html('Foo')
        ->setNodeBinary('/home/nil/.nvm/versions/node/v20.11.1/bin/node')
        ->setNpmBinary('/home/nil/.nvm/versions/node/v20.11.1/bin/npm')
        ->save('invoice.pdf');

and

    Browsershot::html('Foo')
        ->setIncludePath('$PATH:/home/nil/.nvm/versions/node/v20.11.1/bin')
        ->save('invoice.pdf');

but I do still have the same error saying Node is not found. I have double checked the Path and saw that Node and Npm are there.

I am using Sail + WSL2.

Do you have any guides I could follow? Thank you so much

0 likes
2 replies
gych's avatar

When you run this in the terminal does it return the expected versions?

/home/nil/.nvm/versions/node/v20.11.1/bin/node --version
/home/nil/.nvm/versions/node/v20.11.1/bin/npm --version
Donny5300's avatar

@nillegal I ran into the same issue. The instance of Browsershit can be manual configured on a slightly different way. The solution that workes for me is:


        $pdf = Pdf::view('notifications.financial.invoice_pdf')
            ->withBrowsershot(function (Browsershot $browsershot) {
                return $browsershot
                    // Target the whole dir as both node and NPM are there located
                    ->setIncludePath('~/.nvm/versions/node/v20.11.1/bin')
                    
                    // Or both by hand if they are in different folders
                    ->setNodeBinary('~/.nvm/versions/node/v20.11.1/bin/node')
                    ->setNpmBinary('~/.nvm/versions/node/v20.11.1/bin/npm');
            })
            ->save('invoice.pdf');

Works like a charm if you ask me

Please or to participate in this conversation.