storybook: [Bug]: Angular Library requires Zone.js

Describe the bug

When creating an Angular library with Storybook, an error is thrown “NG0908: In this configuration Angular requires Zone.js”. Zone is installed in the project.

I initially found this using NX to create a control component library. However, I was able to reproduce it without NX using just Angular libraries.

Reproduction link

https://stackblitz.com/edit/stackblitz-starters-34gmr3qu?file=package.json

Reproduction steps

  1. Create a new Angular project
  2. Add storybook
  3. Create a button (for example) component
  4. Add a story for the component
  5. Run storybook
  6. Observe that as long the component is in the “app,” the story is displayed as expected.
  7. Create a library for your control
  8. Create a component (button, perhaps)
  9. Create a story for said component
  10. Wire up the angular.json to serve Storybook for the new library
  11. Run Storybook for the library
  12. Observe the story is not served. Instead, you are presented with an error reading “NG0908: In this configuration Angular requires Zone.js”

System

Angular ^19.1.0
Storybook ^8.6.0

Additional context

I have provided scripts in the package.json of this StackBlitz for an easy comparison. Please run npm run control:storybook to see the error

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 21
  • Comments: 19 (6 by maintainers)

Most upvoted comments

@shilman just tried with 8.6.2 and without explicitly import zone.js into preview.ts the error is still present

Same problem, storybook stops working after update to 8.6+ (in zone mode).

Polyfills specified in project.json as "polyfills": ["zone.js"], but it does not help.

Workaround:

// preview.ts

import {Preview} from '@storybook/angular';
import 'zone.js';

// Other code

@emopti-jrufer Thank you for looking into this. This morning I realized I am starting a new project, so I probably don’t need zone.js. So I did some digging and found feature request #28403. Storybook version 8.6.0 (published 2 days ago) now supports zoneless!

If you are running into this issue trying to use Storybook in an Nx Angular library, the solution I found was:

  1. In the project.json for the library, add "experimentalZoneless": true, to both the “storybook” and “build-storybook” target.
  2. In the test-setup.ts for the library, change
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';

setupZoneTestEnv({
  errorOnUnknownElements: true,
  errorOnUnknownProperties: true,
});

to

import { setupZonelessTestEnv } from 'jest-preset-angular/setup-env/zoneless';

setupZonelessTestEnv({
  errorOnUnknownElements: true,
  errorOnUnknownProperties: true,
});

https://github.com/nrwl/nx/issues/26362

Building a Lib w/ Angular 18, and Storybook 8.6.4. My solution to this problem was to use the "experimentalZoneless": true, config to the options of build-storybook and storybook.

While this is nice (and works for zoneless libs). It would be nice if this toggle also injects zone.js if it’s set to false. Making managing ‘zoneless’ a bit simpler.

And to add to that, if it’s not set it should default to false and just inject zone.js for backward compability

We have added zoneless support in 8.6: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#angular-support-experimental-zoneless-support

We should update our Angular documentation as well, cc @jonniebigodes

@valentinpalkovic I am using an angular library in Nx repo and your solution unfortunately did not work, as the builder (or executor in Nx) does not provide polyfills options. Also, adding polyfills to storybook.options does not work as the schema does not provide polyfills. However, when I added "experimentalZoneless": true inside storybook.options it worked fine.

If you are not on zoneless mode, your angular.json configuration looks something like this:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "angular-v16": {
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:application",
          "options": {
            "polyfills": [
              // 👇 Zoneless polyfill should exist
              "zone.js"
            ],
            ...
          },
        "storybook": {
          "builder": "@storybook/angular:start-storybook",
          "options": {
            "browserTarget": "angular-v16:build",
            // ...
          }
        },
        "build-storybook": {
          "builder": "@storybook/angular:build-storybook",
          "options": {
            "browserTarget": "angular-v16:build",
           // ...
          }
        }
      }
    }
  }
}

The storybook and the build-storybook executors define a browserTarget, and this browserTarget (in this case angular-v16:build) should have architect.build.options.polyfills = ["zone.js"] defined. Otherwise, your own application would also not import the zone.js polyfill. If this is not defined, either add it there or try to add it in storybook.options.polyfills = ['zone.js'] and in build-storybook.options.polyfills = ['zone.js']

As a workaround the import zone.js can be added to the preview.ts|js. For me I just imported my projects polyfills.

// .storybook/preview.ts

import {setCompodocJson} from '@storybook/addon-docs/angular';
import docJson from '../documentation.json';

setCompodocJson(docJson);

import '../src/polyfills';

Tracked down the source of the issue.

The following was removed from code/frameworks/angular/src/client/globals.ts.

/** Zone JS is required by Angular itself. */
import 'zone.js';

https://github.com/storybookjs/storybook/commit/10bb8e0afc765a6069b36df2a326f56e69696546#diff-0c21257608d891851ff3890160ecd56a82d7f067127b2fdaaed982a446227089