How to Debug a Server–Side Rendered Storefront (DRAFT)
When a runtime error occurs in the server-side rendered Javascript application, it outputs only a stacktrace in the console of the Node.js process (not in the browser) and sometimes it can be difficult to deduce the cause cause of an error. But fortunately, you can use Node.js debugging to take advantage of standard debugging techniques like inspecting variables in code, breakpoints, etc.
Node.js Debugging in VS Code
- Build your Javascript Storefront and start the Node server with it
- In VS Code, open the
Debug
panel (Ctrl+Shift+D) - Unless you have done it before, add a new configuration, as follows:
- from the dropdown choose option
Add configuration
- then in the automatically-opened file
launch.json
choose the optionNode.js Attach to process
from the list of suggestions and save the file
- from the dropdown choose option
- Run debugging with above configuration. In opened popup in the center of the window choose the process with path to your Javascript Storefront (i.e.
/dist/ssr/server.js
)
How to Set Breakpoint on Exceptions
- In the bottom of the
Debug
panel, expand the sectionBreakpoints
and select checkboxAll exceptions
. - Open the storefront in the browser (i.e.
localhost:4200
) and VS Code should stop in a line that throws a runtime error.
References
- Docs: https://code.visualstudio.com/docs/nodejs/nodejs-debugging
- Video: https://www.youtube.com/watch?v=2oFKNL7vYV8