To publish a React component library, define a small public API, build it for package consumers, declare its JavaScript, type, and CSS entry points, then test the packed package in a separate React app before releasing it to npm. The exact toolchain is a choice; the package contract—what consumers can import and how they use it—must be explicit.
1. Decide what the package promises
Start by choosing a coherent set of components and deciding how another project will consume them. These choices affect your source layout, build, metadata, and documentation, so settle them before configuring output.
- Public API: Decide whether users import components from one root entry, such as
@acme/ui, or from documented subpaths such as@acme/ui/button. Export only supported components and utilities. - Compatibility: State the React versions you support, the JavaScript module formats you provide, and any runtime assumptions. Declare React as a peer dependency when the consuming application should provide it.
- Styling contract: Explain whether consumers import a package stylesheet, use another styling mechanism, or receive unstyled components and design tokens.
- Package contents: Keep consumer-facing build files distinct from source, tests, stories, and examples. Include only files consumers need, plus documentation such as the README and license.
Document these decisions in the README and package metadata. A package is an interface, not just a folder of reusable source files.
2. Organize the source and public exports
Keep the library’s public entry point easy to find and review. For example, component modules can live under src/components/, while src/index.ts re-exports the supported API:
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
export { Button } from "./components/Button";
export type { ButtonProps } from "./components/Button";
Use the file extensions and syntax expected by your chosen build system; the example is illustrative. Avoid exporting internal helpers by default. If a component is not part of the documented API, consumers should not need to depend on its source path.
3. Build library output, not an application
An application build produces files intended to run as an app. A library build instead packages importable modules for other projects. Vite’s library mode uses build.lib to define entry files and supports configurable output formats. Its documented examples use ES and UMD formats for one entry, and ES and CommonJS for multiple entries. Choose formats for actual consumers rather than emitting every possible format. See Vite’s library-mode documentation.
For a browser-oriented component library, externalize dependencies that should be provided by the consumer. Vite specifically gives React as an example of a dependency to externalize. Bundling another copy of React into a component package can create dependency and runtime problems; declare the supported React peer dependency range and make sure the build does not include React in the library bundle.
TypeScript libraries also need declaration files so editors and TypeScript consumers can understand the public props and exports. Configure declaration generation using the selected bundler and TypeScript version, then point package metadata at the generated declarations. Vite’s library-mode guide covers JavaScript output and package metadata, not a complete declaration-generation recipe, so check the relevant tooling instructions for the setup you choose.
4. Make package metadata match the files
Package metadata tells Node.js and other tooling which built files represent the supported interface. Node.js recommends using the exports field for new packages. Once you define it, undeclared package subpaths are encapsulated and cannot be imported through normal package resolution. That makes the supported API clearer, but means every path consumers should use must be listed. Read Node.js package entry-point guidance.
A package configuration might include a package name, version, license, files allowlist, module type, entry conditions, and type declarations. Treat this as a schematic example; use paths and formats that match your actual build output:
Rank #3
{
"type": "module",
"files": ["dist"],
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./style.css": "./dist/style.css"
}
}
If you support both ESM and CommonJS, provide conditions that point to real files in the corresponding formats. Confirm extensions as well: Vite notes that output extensions can depend on the package’s type setting. Do not add a main or module field that points to a nonexistent or differently formatted file. Every declared export must exist in the packed artifact.
5. Choose a clear CSS delivery path
React does not prescribe one way to include CSS; the project and its build tool determine the mechanism. Tell consumers exactly how to get the library’s styles. With Vite, imported CSS can be emitted as a stylesheet alongside the JavaScript, and the package can expose it as a subpath such as @acme/ui/style.css. See React’s guidance on adding styles and Vite’s CSS support in library mode.
Document the consumer import, for example import "@acme/ui/style.css";, only if that is the path your package actually exports. During release validation, confirm the CSS file is included and resolves from a consuming app; a working JavaScript import alone does not prove the styles are available.
Rank #4
6. Use stories to make component states visible
A Storybook story describes a rendered component state using arguments; for React, those arguments correspond to props. Stories make intended usage and visually important edge cases inspectable outside the application. Storybook’s React/Vite framework supports isolated component development and testing. Its documented requirements are React 16.8 or later and Vite 5 or later; these are version-sensitive, so check the requirements for the Storybook release you install. See Storybook for React & Vite.
Create stories for states that help users understand the API and help maintainers spot regressions:
- Default appearance and primary variants.
- Disabled, loading, error, or other meaningful interaction states.
- Long labels, dense content, or other layout stress cases.
- Relevant theme, viewport, or responsive contexts.
Story files use component metadata and named story exports; Storybook controls can change arguments interactively, and a story’s play function can describe interaction scenarios. See Storybook’s guide to writing stories. Stories explain behavior; they complement rather than replace automated tests.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsBest Value
7. Test the package that consumers will install
Test at more than one level: component tests for behavior, type checking for public props and declarations, and stories for visual and interactive states. Then build the package and try the packed output in a separate, minimal React project. This consumer-project check is practical release advice: it catches mismatches between the files on disk and the package contract before users encounter them.
- Build the library using its release configuration.
- Create the package tarball using your package manager’s pack command and inspect its contents. Check that the files allowlist includes the intended JavaScript, declarations, CSS, and documentation.
- Install that tarball into a clean React consumer project, rather than relying only on workspace aliases or source imports.
- Import the documented root entry and any supported subpaths. Confirm module resolution works and that TypeScript can find the declarations.
- Import the documented stylesheet and verify that styles apply. Check that the consumer has the required peer dependencies installed.
- Run the consumer’s build and tests. Resolve missing files, undeclared imports, or format errors before publishing.
A workspace development setup can hide packaging problems by making source files or dependencies available in ways that will not exist for an npm user. Testing the packed artifact exercises the files and paths described by your package metadata.
8. Publish and maintain the release
Before publishing, review the package name and version, license, README, release notes, dependency declarations, supported exports, and packed files. Confirm the package’s intended scope and access settings, then follow npm’s current account, authentication, and publication requirements. Those rules and command options can change; verify them in the current npm documentation rather than relying on old command examples.
After a release, treat changes to exports, styling, peer dependency ranges, and module formats as changes to the consumer-facing contract. Document breaking changes and verify the same clean installation path for each release.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




