惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
D
Docker
腾讯CDC
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
博客园 - 三生石上(FineUI控件)
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
IT之家
IT之家
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
博客园 - 【当耐特】

Nx Blog

Sharing Tailwind CSS Styles Across Apps in a Monorepo | Nx Blog How SiriusXM Stays Competitive by Iterating and Getting to Market Fast | Nx Blog Agentic Experience Is the New Developer Experience | Nx Blog Nx Joins the Linux Foundation and the Agentic AI Foundation | Nx Blog A Monorepo Is NOT a Monolith | Nx Blog Why we deleted (most of) our MCP tools | Nx Blog Teach Your AI Agent How to Work in a Monorepo | Nx Blog How Broadcom stays efficient and nimble with monorepos | Nx Blog Why Monorepos are King in the Age of AI | Nx Blog Nx 2026 Roadmap: Expanding Agent Autonomy, Improving Performance, Better Polyglot and More | Nx Blog End to End Autonomous AI Agent Workflows with Nx | Nx Blog Autonomous Agents at Scale | Nx Blog Scaling 700+ Projects: How Nx Became a 'No-Brainer' for Caseware | Nx Blog Configure Tailwind v4 with Angular in an Nx Monorepo | Nx Blog The Missing Multiplier for AI Agent Productivity | Nx Blog A Year of Nx Webinars | Nx Blog Wrapping Up 2025 | Nx Blog Nx 22.3 Release: Angular 21 Support, tsgo Compiler, and Prettier v3 | Nx Blog Nx Cloud Release: Agent Resource Usage | Nx Blog Nx Platform Outperforms DIY Cache by 5x | Nx Blog An Nx Carol: Past, Present, and Future of Your Monorepo | Nx Blog Nx 22.1 Release: Terminal UI on Windows, Storybook 10, Vitest 4, and more! | Nx Blog The Compounding Effect: How Nx Features Multiply Performance Gains | Nx Blog 10 Monorepo Myths Debunked: Separating Fact from Fiction | Nx Blog Nx Cloud Release: Enterprise Task Analytics | Nx Blog Book - React for Enterprise: Timeless Architecture for Enterprise Apps | Nx Blog Beyond Remote Cache: Unlock 70% More CI Performance | Nx Blog Nx 22 Release: Expanding the build platform | Nx Blog What's the Point of Generating All This Code If You Can't Merge It? | Nx Blog What's New in Nx Self-Healing CI | Nx Blog
Watch and Rebuild Storybook Dependencies with Nx | Nx Blog
Juri Strumpflohner · 2025-10-30 · via Nx Blog

This came up in our Discord: A Storybook setup in a library that depends on another buildable package in the monorepo. The problem? Whenever you run Storybook and change something in the buildable package, you won't see the effect right away. You need to manually rebuild it, and only then will Storybook pick up the changes.

Let me show you how we can fix this with Nx's workspace watching feature. (Demo repo included in the links at the very end.)

The Problem: Buildable Libraries and Storybook

I reproduced the setup. Here's what we're working with:

Project graph showing Storybook library depending on UI library

We have a Storybook library (feat-create-orders) that depends on a buildable library (ui). The buildable library is configured to point to its pre-built output in the dist folder. This means everyone consuming this library in the monorepo will point to the pre-built version rather than the source files.

Here's what the ui library's package.json looks like:

{
  "name": "@storybooknx/ui",
  "version": "0.0.1",
  "type": "module",
  "main": "./dist/index.js",
  "module": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "exports": {
    "./package.json": "./package.json",
    ".": {
      "@storybooknx/source": "./src/index.ts",
      "types": "./dist/index.d.ts",
      "import": "./dist/index.js",
      "default": "./dist/index.js"
    }
  }
}

Notice how the main entry points point to *.js files. This means we need to keep the dist folder up-to-date whenever we make changes to the source files. Otherwise, Storybook won't reflect our changes.

The Solution: Nx Workspace Watching

Nx actually has a feature that allows you to watch files and automatically run commands when they change. You can find all the details in the Workspace Watching guide in the Nx docs.

The key is the nx watch command which lets you watch specific projects and run a command whenever a change is detected. Here's the basic syntax:

npx nx watch --projects=@storybooknx/feat-create-orders --includeDependentProjects -- nx build-deps @storybooknx/feat-create-orders

For our Storybook setup, we need to watch all dependencies (--includeDependentProjects) of the Storybook package and rebuild them whenever something changes. This way, Storybook's Vite HMR server picks up the changes and refreshes automatically.

Setting It Up: Step by Step

To make this work, we need to configure two targets in our Storybook library's configuration. You can also follow along in my example repository.

1. The build-deps Target

This is a simple no-op target that serves as a hook point. The magic happens through its dependsOn configuration, which tells Nx to build all dependencies of the current project:

packages/feat-create-orders/package.json

{
  "name": "@storybooknx/feat-create-orders",
  "version": "0.0.1",
  "nx": {
    "targets": {
      "build-deps": {
        "dependsOn": ["^build"],
        "executor": "nx:noop"
      }
    }
  }
}

The ^build in dependsOn means: "build all my dependencies first." Since this is a noop executor, it won't do anything itself, but Nx will ensure all dependencies are built before this target completes.

2. The watch-deps Target

This is where the actual watching happens:

packages/feat-create-orders/package.json

{
  "name": "@storybooknx/feat-create-orders",
  "version": "0.0.1",
  "nx": {
    "targets": {
      "build-deps": {
        "dependsOn": ["^build"],
        "executor": "nx:noop"
      },
      "watch-deps": {
        "continuous": true,
        "dependsOn": ["build-deps"],
        "executor": "nx:run-commands",
        "options": {
          "command": "nx watch --projects @storybooknx/feat-create-orders --includeDependentProjects -- nx build-deps @storybooknx/feat-create-orders"
        }
      }
    }
  }
}

Let's break down what's happening:

  • continuous: true - Marks this as a long-running task
  • dependsOn: ["build-deps"] - Ensures dependencies are built before watching starts
  • The command watches the current project and all its dependencies
  • When a change is detected, it runs nx build-deps which triggers rebuilding

3. Update the Storybook Target

Finally, we need to make the Storybook target depend on these new targets:

packages/feat-create-orders/package.json

{
  "nx": {
    "targets": {
      "storybook": {
        "dependsOn": ["^build", "watch-deps"]
      },
      "build-deps": {
        "dependsOn": ["^build"],
        "executor": "nx:noop"
      },
      "watch-deps": {
        "continuous": true,
        "dependsOn": ["build-deps"],
        "executor": "nx:run-commands",
        "options": {
          "command": "nx watch --projects @storybooknx/feat-create-orders --includeDependentProjects -- nx build-deps @storybooknx/feat-create-orders"
        }
      }
    }
  }
}

Now when you run nx storybook feat-create-orders, Nx will:

  1. Build all dependencies (^build)
  2. Start the watch command in parallel (watch-deps)
  3. Serve Storybook

Copy Configuration from Nx Console

If you're using a Vite or React app with Nx, you likely already have the watch-deps target configured automatically. You can easily copy the target configuration from Nx Console! Just open the project panel, navigate to the watch-deps target, and click "Copy Target Configuration." Then paste it into your Storybook library's package.json and adjust as needed. Check the video for the details

See It in Action

Let me show you the final developer experience:

When you run:

nx storybook feat-create-orders

You'll see the Nx terminal split into two panels:

  • Top panel: Storybook dev server
  • Bottom panel: The watch-deps process monitoring for changes

Nx TUI showing the storybook run and watch in 2 panels

When you make any change in the UI package, you'll see the bottom panel running the watch-deps command update, rebuild the package and hence the Storybook Vite HMR server will pick it up and show the live result.

Watch-deps and Build-deps by Default in Nx v22

nx watch is powerful and crucial in a monorepo to maintain good DX by automatically rebuilding buildable packages. Nx already adds this to applications and other buildable packages. While recording the video (you can actually see it in the video itself), I realized this should be the default experience for Nx + Storybook.

I submitted a PR right after finishing the video, and this now ships by default with Nx v22. The power of open source!

Conclusion

Setting up automatic rebuilding for your Storybook dependencies transforms the developer experience from frustrating to seamless. No more manual rebuilds, no more stale changes - just edit your code and see it update in Storybook immediately.

The combination of Nx's workspace watching, intelligent caching, and task orchestration makes this setup not only possible but performant. Give it a try and experience the difference!


Learn More

Also, make sure to check out: