Debugging a Server–Side Rendered Storefront
Note: Spartacus 4.x is no longer maintained. Please upgrade to the latest version.
Note: Spartacus 4.x was tested with SAP Commerce Cloud versions 1905 to 2205. Spartacus 4.x has not been verified to work with (and is not guaranteed to work with) SAP Commerce Cloud 2211 or later releases.
If a runtime error occurs in your server-side rendered Spartacus storefront application, the application only outputs a stacktrace in the console of the Node.js process, not in the browser, and it can sometimes be difficult to determine the cause of the error. Fortunately, you can use Node.js debugging to take advantage of standard debugging techniques, such as inspecting variables in the code, inspecting breakpoints, and so on.
Debugging Node.js in VS Code
The following steps describe how to use Node.js debugging in Visual Studio Code.
- Build your Spartacus storefront app and start the Node server with it.
-
In Visual Studio Code, open the Run view.
For more information, see Run view in the VS Code documentation.
-
If you have not already done so, create a new
launch.json
file and selectNode.js
as the environment.For more information, see Launch configurations in the VS Code documentation.
-
In the new
launch.json
file, clickAdd Configuration
and selectNode.js: Attach to Process
.For more information, see Add a new configuration and Attaching to Node.js in the VS Code documentation.
- Run the debugger using the
Attach by Process ID
configuration, and when asked toPick the node.js process to attach to
, select the process with a path to your Spartacus storefront app, such as/dist/ssr/server.js
.
For more information, see Getting started with Node.js debugging in VS Code on Visual Studio Code’s official YouTube channel.
Setting Breakpoints on Exceptions
- At the bottom of the Run view, expand the Breakpoints section and select the
All exceptions
checkbox. - Open the storefront app in a browser (for example,
localhost:4200
) and VS Code should stop on a line that throws a runtime error.