Vite cannot @import a CSS file from an npm package and prints a "Missing specifier" error
Missing "./dist/theme.css" specifier in "some-package" packageShort 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.
Fix it fast
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)"2Match what you see, then jump to its fix
- 1
Missing "./dist/theme.css" specifier in "some-package" packageThe exports map of the package does not list that fileFix - 2
No known conditions for "./dist/theme.css" specifier in "some-package" packageThe key exists, but none of its conditions can matchFix - 3
Unable to resolve `@import "some-package"` from /app/srcThe @import names the package only, with no file pathFix
- 1
What does the error look like?
Missing "./dist/theme.css" specifier in "some-package" packageNo known conditions for "./dist/theme.css" specifier in "some-package" packageUnable 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/*.css2Fix
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 buildConfirmed by nodejs.org
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 devConfirmed by raw.githubusercontent.com
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 devConfirmed by raw.githubusercontent.com
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix