Vite says it failed to resolve the entry for a package in node_modules
Failed to resolve entry for packageShort answer
Vite picks a package's entry from its exports field first, then from main fields such as browser and module. If exports has no condition Vite can use, or the file that main or exports points to is missing, resolution fails. Look at the package.json, then fix the version, add a resolve rule, or alias the exact file.
Fix it fast
1Find out which cause you have
Open the package's own package.json in node_modules and compare main, module and exports with the files that actually exist in the folder.
cat node_modules/<package>/package.json ls node_modules/<package>2Match what you see, then jump to its fix
- 1
package.json points to ./dist/index.js but ls shows no dist folderThe file named in main or exports does not exist in the installed packageFix - 2
package.json has an exports object whose keys are only node or requireexports defines conditions that Vite does not matchFix - 3
You changed or upgraded the package but the same error staysA stale pre-bundle cache or a linked packageFix
- 1
What does the error look like?
Failed to resolve entry for packageThe package may have incorrect main/module/exports specified in its package.json
Cause 1 of 3
The file named in main or exports does not exist in the installed package
1Check
Compare each path in exports and main with the real files. Node's rules say each target in exports must start with ./ and stay inside the package.
node -e "const p=require('./node_modules/<package>/package.json');console.log(p.main,p.module,JSON.stringify(p.exports))"2Fix
Reinstall the package at a version that ships its build output, or use the package's published build instead of a git or linked source copy.
npm install <package>@latest
npx vite --force3Verify
The dev server starts without the resolve error.
npx viteConfirmed by nodejs.org
Cause 2 of 3
exports defines conditions that Vite does not match
1Check
exports takes precedence over main. Vite's default resolve.conditions are module, browser and development or production, and import, require and default always apply when valid. A package with only a node condition has no match.
node -e "console.log(JSON.stringify(require('./node_modules/<package>/package.json').exports,null,2))"2Fix
Add the condition the package uses, or alias the package name to the exact file you want.
// vite.config.ts
import path from 'node:path'
export default defineConfig({
resolve: {
alias: {
'<package>': path.resolve(__dirname, 'node_modules/<package>/dist/index.js'),
},
},
})3Verify
Restart the dev server.
npx vite --forceConfirmed by vite.dev
Cause 3 of 3
A stale pre-bundle cache or a linked package
1Check
Vite caches pre-bundled dependencies in node_modules/.vite and treats linked packages outside node_modules as source code.
ls node_modules/.vite2Fix
Re-run pre-bundling with --force or delete the cache folder. Add a linked package that is not ESM to optimizeDeps.include.
rm -rf node_modules/.vite
npx vite --force3Verify
The error no longer appears when the dev server starts.
npx viteConfirmed by vite.dev
How do I stop it coming back?
- Pin dependency versions with a lock file so a broken release does not slip in with an update.
- Check a new package for its main and exports fields before adding it.
- Run vite --force after changing dependencies.
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
Web apps
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix