Preventing Duplicate React Bundling in a Turborepo Monorepo

I’m using Turborepo to manage a monorepo with multiple packages. One of my packages is:

  • @repo/shadcnui (a shared UI library)

    • It declares react@19 as a dependency.

I also have an app package in the same repo that lists React as a dependency. When I import components from @repo/shadcnui into my app and run the build (e.g., with Next.js/Vite/Webpack), will React end up being bundled twice (once for the UI package and once for the app)? Or does Turborepo/your bundler automatically dedupe React into a single copy? How can I ensure React is only included once in my final bundle?

Hi @msgdanin-4551, welcome to the Vercel Community!

I see. I think using peerDependencies for your UI package should fix this issue.

You should be able to fix the issue by adding the following to your @repo/shadcnui package.json:

{
  "peerDependencies": {
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  },
  "devDependencies": {
    "react": "^19.0.0",
    "react-dom": "^19.0.0"
  }
}
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.