Building an application and building a library are different jobs with different constraints. An app is a leaf — nothing imports it, so bundling everything in is correct. A library is a node in someone else's graph, and every decision you make becomes their problem.
Switching the build target
An app build starts from index.html. A library build starts from a module:
build: {
lib: {
entry: path.resolve(__dirname, 'src/index.ts'),
name: 'MyLib', // required for umd/iife: the global variable name
fileName: 'my-lib',
formats: ['es', 'cjs'],
},
}
Choosing output formats
| Format | Consumed by | Notes |
|---|---|---|
es | Bundlers, modern Node, browsers | Tree-shakeable. Always ship this. |
cjs | require in Node tooling | Add when consumers may be CommonJS. |
umd / iife | A script tag with no build step | Needs name and output.globals. |
Ship es always; add the others only when you have a consumer that needs them. Each format is another artifact to test and another way to be broken.
Multiple entries
A single entry means importing one helper pulls the entire package into the consumer's module graph, and tree-shaking is not always able to remove the rest. Named entries give consumers a smaller surface:
lib: {
entry: {
index: 'src/index.ts',
utils: 'src/utils.ts',
},
formats: ['es'],
}
Then declare the map in package.json so the paths are stable and enforceable:
{
"type": "module",
"files": ["dist"],
"exports": {
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
"./utils": { "types": "./dist/utils.d.ts", "import": "./dist/utils.js" },
"./styles.css": "./dist/style.css"
},
"sideEffects": false
}
Two things worth understanding here:
exportsis an allow-list. Anything not listed cannot be imported, which stops consumers depending on your internal file layout and then breaking when you move a file.sideEffects: falsetells bundlers unused exports are safe to drop. Only claim it if it is true. If importing a module registers something, patches a global, or injects CSS, lying here causes the registration to vanish from consumer builds — a bug that appears only in their production output.
external and peerDependencies: the important part
rollupOptions: {
external: ['react', 'react-dom', 'react/jsx-runtime'],
output: {
globals: { react: 'React', 'react-dom': 'ReactDOM' }, // umd/iife only
},
}
{
"peerDependencies": { "react": ">=18" },
"devDependencies": { "react": "^18.3.1" }
}
The rule: every peerDependency must be in external. peerDependencies states "the consumer supplies this"; external is what actually implements that statement in the build. Declare the peer and forget the external, and you ship a copy of the framework anyway while claiming you do not.
Do not forget react/jsx-runtime. The automatic JSX transform imports from it, so leaving it out silently bundles a piece of React even when react itself is external.
Why a duplicate framework copy breaks
This is the failure that generates the most confusing bug reports, so it is worth understanding mechanically rather than as a rule to follow.
React's hooks do not live in the component. Calling useState reads a module-level pointer to the currently rendering component, set by the renderer just before it calls your function. That pointer is per-copy state inside the React module instance.
If your library bundles its own React:
- The app renders using copy A. Copy A sets its internal pointer.
- Your library's component calls
useStatefrom copy B. - Copy B's pointer was never set — it is null.
- You get "Cannot read properties of null" or the invalid-hook-call error.
The error message points at hook rules, so people go looking for a conditional hook that does not exist. The real cause is two module instances.
The same shape breaks anything built on module-level identity:
- Context:
createContextproduces a unique object. A provider from copy A cannot be found by a consumer looking for copy B's context, so the consumer silently gets the default value. No error, just wrong data — which is worse. - Instance checks and shared registries across two copies of any singleton-style library.
Two ways to confirm it: npm ls react in the consumer project showing more than one resolved version, or the library's dist containing recognisable framework source. In a local-link setup the same duplication appears through the linked package's own node_modules, which is why resolve.dedupe exists for the consumer side.
Type declarations
TypeScript output from Vite does not include .d.ts files. Without them, consumers get any at every boundary, and the package looks broken to a typed codebase.
Emit them with a declaration plugin or tsc --emitDeclarationOnly, then point types in each exports entry at the emitted file. Check the built package before publishing rather than after:
npm pack --dry-run # exactly which files would ship
Common publishing mistakes this catches: dist missing from files, source maps referencing paths that do not exist in the tarball, and test fixtures shipped by accident.
CSS in a library
If your components import CSS, the build emits a stylesheet the consumer must include. Say so in the README and expose it through exports. Injecting styles at import time instead is convenient and costs the consumer any ability to control order or opt out — and it conflicts with sideEffects: false.
Exercise: A consumer reports the invalid-hook-call error only when they use your component library, and npm ls react shows one version. Name the field most likely missing from your build config and explain why one resolved version does not rule out two copies at runtime.