Skip to content

localStorage undefined Angular Server Side Rendering

Posted in Javascript

Here’s a pretty quick fix to get localStorage available in your Angular Server-Side Rendering application.

This article assumes you followed the Angular Server-Side Rendering Story:

Step 1

Install npm install localstorage-polyfill

Step 2

Add this to your server.ts file:

Step 3

Rebuild your project. npm run build:ssr and serve locally as npm run serve:ssr. All localStorage errors should be suppressed now.


You don’t have to change ANY part of your actual Angular code. The above approach makes sure localstorage-polyfillis available for global use.

If you’re wondering how to run your SSR (Server Side Rendering) app in production using Nginx, check out this other article I wrote: Serving Angular SSR via Nginx Proxy