{"record":{"id":"f90ec1fdce1020ff","repo":"vitejs/vite","slug":"the-following-dependencies-are-imported-but-could","errorCode":null,"errorMessage":"The following dependencies are imported but could not be resolved:\n\n  ${missingIds mapped to \"id (imported by importer)\"}\n\nAre they installed?","messagePattern":"The following dependencies are imported but could not be resolved:\n\n  (.+?)\n\nAre they installed\\?","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"packages/vite/src/node/optimizer/index.ts","lineNumber":471,"sourceCode":"  await fsp.rm(depsCacheDir, { recursive: true, force: true })\n}\n\n/**\n * Initial optimizeDeps at server start. Perform a fast scan using esbuild to\n * find deps to pre-bundle and include user hard-coded dependencies\n */\nexport function discoverProjectDependencies(environment: ScanEnvironment): {\n  cancel: () => Promise<void>\n  result: Promise<Record<string, string>>\n} {\n  const { cancel, result } = scanImports(environment)\n\n  return {\n    cancel,\n    result: result.then(({ deps, missing }) => {\n      const missingIds = Object.keys(missing)\n      if (missingIds.length) {\n        throw new Error(\n          `The following dependencies are imported but could not be resolved:\\n\\n  ${missingIds\n            .map(\n              (id) =>\n                `${colors.cyan(id)} ${colors.white(\n                  colors.dim(`(imported by ${missing[id]})`),\n                )}`,\n            )\n            .join(`\\n  `)}\\n\\nAre they installed?`,\n        )\n      }\n\n      return deps\n    }),\n  }\n}\n\nexport function toDiscoveredDependencies(\n  environment: Environment,","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/optimizer/index.ts#L453-L489","documentation":"Thrown by `discoverProjectDependencies` after the dependency scanner (esbuild/rolldown scan of entry HTML/JS) finds imports that resolve to no installed package. Each missing id is listed with its importer so you can locate the offending import. It is a hard stop: dependency optimization cannot proceed with unresolvable bare imports.","triggerScenarios":"`scanImports` populates a `missing` map; in index.ts:469 if `Object.keys(missing).length` is non-zero, the error lists each `id (imported by importer)`. Occurs on `vite` / `vite dev` / `vite build` startup when source code imports a bare specifier that node resolution cannot find from the importer's directory.","commonSituations":"Forgot `npm install` after pulling code or switching branches. Package is in `dependencies` of a sibling workspace but not hoisted/installed in this one. Typo in import specifier. Conditional/optional import that should be excluded via `optimizeDeps.exclude` or externalized. Monorepo with missing symlink after a partial install. Using a package that is `devDependencies` only and running in a prod-only install.","solutions":["Install the listed package: run `npm install <missing-id>` (or pnpm/yarn equivalent) matching the message.","If the import is intentional but should not be bundled, add it to `optimizeDeps.exclude` or `build.rollupOptions.external`.","If it is a virtual/conditional import, guard it so the scanner cannot see it, or mark it external.","For workspaces, re-run the install with workspace linking (`pnpm install`, `npm install --workspaces`) to fix missing hoists.","Fix typos in the specifier or update the import path to a real subpath that the package exports.","Clear the Vite cache (`node_modules/.vite`) after fixing installs to drop stale optimization metadata."],"exampleFix":"// before: app.ts imports 'lodash.debounce' but it is not installed\nimport debounce from 'lodash.debounce'\n\n// after: install then keep the import\n// $ npm install lodash.debounce\nimport debounce from 'lodash.debounce'","handlingStrategy":"validation","validationCode":"import { createRequire } from 'node:module'\nimport path from 'node:path'\nconst require = createRequire(import.meta.url)\n\nfunction assertResolvable(specifiers: string[], fromDir: string) {\n  for (const s of specifiers) {\n    if (!s.startsWith('.') && !path.isAbsolute(s)) {\n      try { require.resolve(s, { paths: [fromDir] }) }\n      catch { throw new Error(`Bare import '${s}' is not resolvable from ${fromDir}; install it before build.`) }\n    }\n  }\n}\n// run over your entry source imports pre-build","typeGuard":null,"tryCatchPattern":"try {\n  await build()\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('The following dependencies are imported but could not be resolved')) {\n    console.error('Missing deps detected. Running install...')\n    // optionally: execSync('npm install') then retry once\n  }\n  throw e\n}","preventionTips":["Always install after switching branches: run `npm ci` (or equivalent) before `vite`/`vite build`.","Add a CI step that fails if `git diff --name-only` includes package.json without a lockfile change.","Lint for imports of packages absent from package.json dependencies."],"tags":["dependencies","resolver","startup","install"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}