Maaz Zindani

Loading the index…

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

Vite says it failed to resolve the entry for a package in node_modules

ErrorFailed to resolve entry for package

Short 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.

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

Fix it fast

  1. 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>
  2. 2Match what you see, then jump to its fix

    1. 1package.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. 2package.json has an exports object whose keys are only node or requireexports defines conditions that Vite does not matchFix
    3. 3You changed or upgraded the package but the same error staysA stale pre-bundle cache or a linked packageFix

What does the error look like?

  • Failed to resolve entry for package
  • The 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 --force

3Verify

The dev server starts without the resolve error.

npx vite

Confirmed by nodejs.org

Did this fix it?

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 --force

Confirmed by vite.dev

Did this fix it?

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

2Fix

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 --force

3Verify

The error no longer appears when the dev server starts.

npx vite

Confirmed by vite.dev

Did this fix it?

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 fix

Next step

Want this handled for good?

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