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.

  1. Build your Spartacus storefront app and start the Node server with it.
  2. In Visual Studio Code, open the Run view.

    For more information, see Run view in the VS Code documentation.

  3. If you have not already done so, create a new launch.json file and select Node.js as the environment.

    For more information, see Launch configurations in the VS Code documentation.

  4. In the new launch.json file, click Add Configuration and select Node.js: Attach to Process.

    For more information, see Add a new configuration and Attaching to Node.js in the VS Code documentation.

  5. Run the debugger using the Attach by Process ID configuration, and when asked to Pick 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

  1. At the bottom of the Run view, expand the Breakpoints section and select the All exceptions checkbox.
  2. Open the storefront app in a browser (for example, localhost:4200) and VS Code should stop on a line that throws a runtime error.