Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

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 show

Fix it fast

Most common cause, 1 of 2

The host app never imports the library's CSS file

In the library, expose the stylesheet through a subpath in package.json exports.

Fix
// library package.json
"exports": {
  ".": "./dist/my-lib.js",
  "./style.css": "./dist/my-lib.css"
}

// host app src/main.ts
import 'my-lib/style.css'
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Vite 8.3, Vite 7, Vite 6
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
npm run build
ls dist/*.css
grep -n "css" package.json

Then find the line you see in this list and open its fix:

  1. 1The host app never imports the library's CSS fileNo build error, the components work, but none of their styles apply
  2. 2The library moved to Vite 6 or later, and style.css got a new namedist holds my-lib.css (or a name taken from build.lib.fileName) and no style.css

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 app
  • dist/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.

Check
grep -rn "my-lib/style.css" src

Fix

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.

Fix
// 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.

Verify
npm run build
ls dist/assets/*.css

Confirmed by vite.dev

Did this fix it?

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.

Check
ls dist/*.css
grep -n "style.css" package.json

Fix

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.

Fix
// 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.

Verify
npm run build
ls dist/style.css

Confirmed by v6.vite.dev

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.