spatie / server-side-rendering

Server side rendering JavaScript in a PHP application
https://sebastiandedeyne.com/posts/2018/server-side-rendering-javascript-from-php
MIT License
602 stars 34 forks source link

Issue with Node as an engine #20

Closed codejet closed 6 years ago

codejet commented 6 years ago

I know the readme says that knowledge of SSR is assumed, and so far I have only really worked with SSR in a Ruby on Rails & React context, which worked pretty much out of the box. So my knowledge is shallow and I hope I'm not asking an unnecessary question here...

I've chosen Node as an engine, and if I call the renderer from a Twig template there is no error. But there is also nothing being rendered.

When I dump the result of $result = $this->engine->run($serverScript); it's string(0) "".

I tried putting several different contents into my JS file, also working with npm packages that explicitly support SSR, but the result is always the same. So I'm wondering if something should be rendered in my case, or if there are additional steps necessary that I'm missing.

codejet commented 6 years ago

When doing some further debugging I figured that an error is being thrown which causes nothing or the fallback to be rendered. For some reason the only imported module in the file to be rendered cannot be found even though it is installed and inside the node_modules folder.

An exception has been thrown during the rendering of a template ("The command "/usr/bin/node /tmp/79507934b56dd570c90a3219601d8730.js" failed.

Exit Code: 1(General error)

Working directory: /var/www/web

Output:
================

Error Output:
================
module.js:549
throw err;
^

Error: Cannot find module 'xyz'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/tmp/79507934b56dd570c90a3219601d8730.js:6:14)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
").
codejet commented 6 years ago

OK, I just found out it was a path issue. I can take it from here.