Peer Dependencies and Tree Shaking
Declare React as a peer dependency, enable side-effects-free tree shaking, and test the published package.
Peer Dependencies and Tree Shaking is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are Peer Dependencies?
Peer dependencies declare packages that your library requires but expects the consumer to provide. In your library's package.json, React belongs in peerDependencies, not dependencies. When a user installs your library, npm/yarn does not install React automatically — the user's app already has React, and your library shares that instance.
Why Not Put React in dependencies?
If React is in your library's dependencies, npm may install a second copy of React alongside the consumer's React. Two React instances in the same app break all hooks with the "Invalid hook call" error. By declaring React as a peerDependency, you tell npm "I need React, but use the one the consumer already has."
peerDependenciesMeta for Optional Peers
Some peers may be optional — for example, a library that supports both styled-components and CSS Modules. Mark optional peers using peerDependenciesMeta: { 'styled-components': { optional: true } }. npm will not warn users about missing optional peers, but TypeScript types can still be provided when the optional package is installed.
The sideEffects Field
The sideEffects field in package.json tells bundlers whether your library is safe to tree-shake. Setting "sideEffects": false tells webpack, Rollup, and similar tools that importing any module from your library does not cause side effects — all unused exports can be safely eliminated from the consumer's bundle.
sideEffects with CSS Imports
CSS imports are a common side effect — they inject styles into the page as a side effect of being imported. If your library imports CSS files, set "sideEffects": ["*.css", "*.scss"] to tell bundlers that JS files are safe to tree-shake but CSS files must always be included when imported.
Granular Tree-Shaking with Named Exports
For maximum tree-shakability, export each component as a named export from its own file. In your index.ts, re-export everything: export { Button } from './Button'. When a consumer only imports Button, the bundler can eliminate all other components. Barrel files with re-exports enable this pattern.
Testing Tree-Shaking Effectiveness
Use bundlejs.com to test tree-shaking: paste your import statement and it reports the actual bundle size. The size-limit npm package lets you define size budgets in your package.json and fail CI if the import size exceeds the limit: "size-limit": [{ "path": "dist/index.js", "limit": "10 KB" }].
Publishing Size Budget
Establish a size budget for your library imports. Run npx size-limit in CI to enforce it. Seeing the bundle impact grow over time indicates scope creep. A focused component library should keep individual component imports under a few kilobytes. Alert on regressions before they reach consumers.
CSS-in-JS Libraries in Component Libraries
Avoid styled-components and Emotion in component libraries you intend to distribute. These runtime CSS-in-JS solutions require the consumer to install the same library and version, creating peer dependency conflicts. If the consumer uses Tailwind or plain CSS, they must still bundle the CSS-in-JS runtime. Prefer CSS Modules, which compile to plain CSS at build time.
CSS Modules in Libraries
CSS Modules work well in libraries when combined with a bundler that processes them. tsup and Rollup can inline CSS Modules output as scoped class names. The consumer does not need to configure anything — the styles are already scoped and included in the JS bundle or as a separate CSS file that gets imported.
devDependencies vs peerDependencies
Your own development tools (tsup, TypeScript, testing libraries, Storybook) go in devDependencies — they are only needed during development and are excluded from npm install for consumers. React goes in both peerDependencies (runtime requirement) and devDependencies (needed to compile and test your library locally).
sideEffects Field Meaning
What does setting "sideEffects": false in a library's package.json communicate to bundlers?
Lesson Recap: Peers and Tree Shaking
Declare React in peerDependencies (not dependencies) so consumers share their app's React instance. sideEffects: false enables full tree-shaking of your library. Use sideEffects: ['*.css'] to protect CSS imports from elimination. Named exports from individual files maximize tree-shaking granularity. Enforce a size budget with size-limit in CI. Prefer CSS Modules over CSS-in-JS in libraries to avoid peer dependency conflicts.
Frequently asked questions
Is the “Peer Dependencies and Tree Shaking” lesson free?
Yes — the full text of “Peer Dependencies and Tree Shaking” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Peer Dependencies and Tree Shaking”?
Declare React as a peer dependency, enable side-effects-free tree shaking, and test the published package. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Peer Dependencies and Tree Shaking” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Bundling with Rollup and tsup for Libraries
- ESM and CJS Dual Package Output
- Peer Dependencies and Tree Shaking
- Publishing to npm and Semantic Versioning