Components from a Vite library build show up without their styles
Your error
Failed to resolve import "my-lib/dist/style.css" from "src/main.ts". Does the file exist?2 more lines it can showFix it fast
Most common cause, 1 of 2The host app never imports the library's CSS file
In the library, expose the stylesheet through a subpath in package.json exports.
// library package.json
"exports": {
".": "./dist/my-lib.js",
"./style.css": "./dist/my-lib.css"
}
// host app src/main.ts
import 'my-lib/style.css'Short answer
In Vite 6, 7 and 8 library mode, the library CSS is written to one separate file beside the JavaScript, and the host app gets no styles until it imports that file. Expose it as ./style.css in the package exports and import it once. Since Vite 6 the file takes the package name instead of style.css.
Which cause do you have?
Build the library and look at which CSS file it wrote. Compare that name with the path in the package's exports and with the import in the host app.
npm run build
ls dist/*.css
grep -n "css" package.jsonThen find the line you see in this list and open its fix:
What does the error look like?
Failed to resolve import "my-lib/dist/style.css" from "src/main.ts". Does the file exist?Library components render as plain unstyled HTML in the host appdist/style.css is no longer created after upgrading the library to Vite 6 or later
Cause 1 of 2
The host app never imports the library's CSS file
You seeNo build error, the components work, but none of their styles apply
Check
Library components render unstyled when the host app never imports the stylesheet that a library-mode build writes beside its JavaScript bundles. The Vite guide has library users import that file through the package exports, so search the host app for the import.
grep -rn "my-lib/style.css" srcFix
In the library, expose the stylesheet through a subpath in package.json exports. In the host app, import that subpath once, in the entry file.
// library package.json
"exports": {
".": "./dist/my-lib.js",
"./style.css": "./dist/my-lib.css"
}
// host app src/main.ts
import 'my-lib/style.css'Verify
Build the host app. Its output now contains a CSS asset with the library rules, and the components render styled.
npm run build
ls dist/assets/*.cssConfirmed by vite.dev
Cause 2 of 2
The library moved to Vite 6 or later, and style.css got a new name
You seedist holds my-lib.css (or a name taken from build.lib.fileName) and no style.css
Check
dist/style.css goes missing after an upgrade to Vite 6 or later because the library-mode stylesheet was renamed. Up to Vite 5 it was always style.css; from Vite 6 it is named from the name field in package.json, or from build.lib.fileName when that is a string. An exports entry or a host import that still points at dist/style.css now leads nowhere.
ls dist/*.css
grep -n "style.css" package.jsonFix
Either point the exports entry at the new file name, or keep the old name by setting build.lib.cssFileName to style in the library's Vite config. The second option leaves every existing host import working.
// library vite.config.js
export default defineConfig({
build: {
lib: {
// keep your existing entry, name and fileName here
cssFileName: 'style',
},
},
})Verify
Rebuild the library and confirm dist/style.css is back, then reinstall or relink it in the host app.
npm run build
ls dist/style.cssConfirmed by v6.vite.dev
How do I stop it coming back?
- Keep the CSS behind a fixed exports subpath, ./style.css, so a renamed output file changes one line in the library only.
- Put the line import 'my-lib/style.css' in the install snippet of the library README.
- Set build.lib.cssFileName in the library's Vite config, so the output name no longer follows the package name.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.