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
- Create a new Angular project
- Add storybook
- Create a button (for example) component
- Add a story for the component
- Run storybook
- Observe that as long the component is in the “app,” the story is displayed as expected.
- Create a library for your control
- Create a component (button, perhaps)
- Create a story for said component
- Wire up the angular.json to serve Storybook for the new library
- Run Storybook for the library
- 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)
@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.jsonas"polyfills": ["zone.js"],but it does not help.Workaround:
@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:
"experimentalZoneless": true,to both the “storybook” and “build-storybook” target.to
https://github.com/nrwl/nx/issues/26362
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(orexecutorin Nx) does not provide polyfills options. Also, adding polyfills tostorybook.optionsdoes not work as the schema does not providepolyfills. However, when I added"experimentalZoneless": trueinsidestorybook.optionsit worked fine.If you are not on zoneless mode, your
angular.jsonconfiguration looks something like this:The
storybookand thebuild-storybookexecutors define abrowserTarget, and this browserTarget (in this caseangular-v16:build) should havearchitect.build.options.polyfills = ["zone.js"]defined. Otherwise, your own application would also not import thezone.jspolyfill. If this is not defined, either add it there or try to add it instorybook.options.polyfills = ['zone.js']and inbuild-storybook.options.polyfills = ['zone.js']As a workaround the import
zone.jscan be added to thepreview.ts|js. For me I just imported my projects polyfills.Tracked down the source of the issue.
The following was removed from
code/frameworks/angular/src/client/globals.ts.https://github.com/storybookjs/storybook/commit/10bb8e0afc765a6069b36df2a326f56e69696546#diff-0c21257608d891851ff3890160ecd56a82d7f067127b2fdaaed982a446227089