sveltejs/kit · error · Error

Could not resolve peer dependency "${name}" relative to your

Error message

Could not resolve peer dependency "${name}" relative to your project — please install it and try again.

What it means

import_peer/resolve_peer resolve an optional peer dependency (e.g. @sveltejs/vite-plugin-svelte, typescript) starting from the project root and walking up directories, looking for node_modules/<name>/package.json. If the package is installed nowhere up the tree, Kit throws this error telling you to install it.

Source

Thrown at packages/kit/src/utils/import.js:23

/**
 * Resolves a peer dependency relative to the current working directory.
 *
 * Mainly used to resolve the correct Vite package when an app's SvelteKit is a
 * linked local repository.
 *
 * Duplicated with `packages/adapter-auto`
 * @param {string} dependency
 * @param {string} root
 */
function resolve_peer(dependency, root) {
	let [name, ...parts] = dependency.split('/');
	if (name[0] === '@') name += `/${parts.shift()}`;

	let dir = root;

	while (!fs.existsSync(`${dir}/node_modules/${name}/package.json`)) {
		if (dir === (dir = path.dirname(dir))) {
			throw new Error(
				`Could not resolve peer dependency "${name}" relative to your project — please install it and try again.`
			);
		}
	}

	const pkg_dir = `${dir}/node_modules/${name}`;
	const pkg = JSON.parse(fs.readFileSync(`${pkg_dir}/package.json`, 'utf-8'));

	const subpackage = ['.', ...parts].join('/');

	let exported = pkg.exports[subpackage];

	while (typeof exported !== 'string') {
		if (!exported) {
			throw new Error(`Could not find valid "${subpackage}" export in ${name}/package.json`);
		}

		exported = exported['import'] ?? exported['default'];

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Install the missing package: `pnpm add -D <name>` (or npm/yarn equivalent)
  2. Run the package manager install for the whole workspace (`pnpm install`) so hoisting places it in the root node_modules
  3. Verify it exists: check for node_modules/<name>/package.json in the project root
  4. If using --legacy-peer-deps or npm 6, install peers explicitly

Example fix

// before (package.json devDependencies)
{}
// after
{ "devDependencies": { "@sveltejs/vite-plugin-svelte": "^5.0.0" } }
Defensive patterns

Strategy: try-catch

Validate before calling

const fs = require('fs');
const peerInstalled = (name, root = process.cwd()) =>
  fs.existsSync(`${root}/node_modules/${name}/package.json`);

Type guard

const isPeerAvailable = (name) => {
  try { return !!require.resolve(`${name}/package.json`); } catch { return false; }
};

Try / catch

try {
  const mod = importPeer('@sveltejs/vite-plugin-svelte');
} catch (e) {
  if (e.message.includes('Could not resolve peer dependency')) {
    console.error(`Run: pnpm add -D ${name}`);
    process.exit(1);
  }
  throw e;
}

Prevention

When it happens

Trigger: Running kit commands (dev/build, sync, or tooling that calls import_peer) with a required peer dependency not present in any node_modules between the project and filesystem root — typically the package simply isn't installed, or is only present in a sibling workspace.

Common situations: Fresh clone without `pnpm install`; peer dep listed in package.json but forgotten; npm/peer-dep auto-install disabled (npm <7 or --legacy-peer-deps); monorepo where the dep is in another package's node_modules only.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/029d085a193432b7. Report an issue: GitHub.