Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Vite cannot @import a CSS file from an npm package and prints a "Missing specifier" error

ErrorMissing "./dist/theme.css" specifier in "some-package" package

Short answer

Vite resolves a CSS @import of a package file through that package's exports map. When the map has no key for the path you wrote, the resolver stops with Missing "<path>" specifier. Import a path the package lists, ask the author to add yours, or point a resolve.alias at the file on disk.

  • Applies to Vite 7, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exports map of the package and compare its keys with the path in your @import. If the command prints undefined, the package has no map and the cause is not this one.

    node -p "JSON.stringify(require('./node_modules/some-package/package.json').exports, null, 2)"
  2. 2Match what you see, then jump to its fix

    1. 1Missing "./dist/theme.css" specifier in "some-package" packageThe exports map of the package does not list that fileFix
    2. 2No known conditions for "./dist/theme.css" specifier in "some-package" packageThe key exists, but none of its conditions can matchFix
    3. 3Unable to resolve `@import "some-package"` from /app/srcThe @import names the package only, with no file pathFix

What does the error look like?

  • Missing "./dist/theme.css" specifier in "some-package" package
  • No known conditions for "./dist/theme.css" specifier in "some-package" package
  • Unable to resolve `@import "some-package"` from /app/src

Cause 1 of 3

The exports map of the package does not list that file

1Check

Look for the exact path of your @import among the keys of exports. Once a package defines exports, only the listed subpaths can be imported by anyone; Node.js calls this encapsulation, and Vite resolves CSS imports through the same map.

node -p "Object.keys(require('./node_modules/some-package/package.json').exports)"
grep -n "@import" src/*.css

2Fix

Import one of the paths the package lists. If you maintain the package, add the file to exports; a pattern key such as "./css/*" exposes a whole folder. If it is somebody else's package and nothing listed fits, alias the file with an absolute path and import the alias.

// package.json of the package you maintain
"exports": {
  ".": "./dist/index.js",
  "./dist/theme.css": "./dist/theme.css"
}

// vite.config.js, for a package you do not control
import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vite';

export default defineConfig({
  resolve: {
    alias: {
      'package-theme': fileURLToPath(
        new URL('./node_modules/some-package/dist/theme.css', import.meta.url)
      ),
    },
  },
});

/* src/app.css */
@import 'package-theme';

3Verify

Restart the dev server and run a production build. Both must pass without the error.

npx vite build

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

The key exists, but none of its conditions can match

1Check

The entry for your path is an object whose keys are conditions. Vite's plain CSS resolver asks for the style condition plus development or production. An entry that offers only other conditions, for example node or types, has nothing to give it.

node -p "JSON.stringify(require('./node_modules/some-package/package.json').exports['./dist/theme.css'], null, 2)"

2Fix

For a package you maintain, add a style condition and a default entry, with default last, because Node.js reads the keys in order and default always matches. For a package you do not control, use the alias from the first cause.

"exports": {
  "./dist/theme.css": {
    "style": "./dist/theme.css",
    "default": "./dist/theme.css"
  }
}

3Verify

Restart the dev server so Vite re-reads the package, then reload the page.

npm run dev

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The @import names the package only, with no file path

1Check

A bare package name in a CSS @import is looked up through the package's style field or style condition. A package that ships only a JavaScript entry has neither, so there is nothing to find.

node -p "const p = require('./node_modules/some-package/package.json'); JSON.stringify({ style: p.style, exports: p.exports })"

2Fix

Write the path of the stylesheet that the package documents and lists in its exports, after the package name.

/* src/app.css */
@import 'some-package/dist/theme.css';

3Verify

Save the stylesheet and watch the dev server console stay quiet.

npm run dev

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Read the exports field of a package before importing a deep file from it; copying a path from an old tutorial is how this starts.
  • Prefer the CSS paths named in the package README over guessed folders such as dist or lib.
  • When you publish your own package, list every public stylesheet in exports and keep a style entry for the main one.
  • After upgrading a styling library, rebuild once; a new major version often adds an exports map that old paths do not match.

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.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

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