sveltejs/kit · error · Error

Could not find valid "${subpackage}" export in ${name}/packa

Error message

Could not find valid "${subpackage}" export in ${name}/package.json

What it means

After locating a peer package's directory, resolve_peer walks its package.json `exports` map to find the requested subpackage (e.g. '@sveltejs/kit/src'). If the exports entry is absent or not a resolvable string/conditional object, Kit throws this error.

Source

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

	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'];
	}

	return path.resolve(pkg_dir, exported);
}

/**
 * Resolve a dependency relative to the current working directory,
 * rather than relative to this package (but falls back to trying that, if necessary)
 * @param {string} dependency
 * @param {string} root
 */
export async function import_peer(dependency, root) {
	try {
		return await import(/* @vite-ignore */ pathToFileURL(resolve_peer(dependency, root)).href);
	} catch {

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Update the peer package to a version that declares the subpath export (pnpm update <name>)
  2. Check package.json `exports` of the installed package for the exact subpath spelling
  3. Fix typos in the requested subpath (case-sensitive, leading '.' forms)
  4. Reinstall clean (rm -rf node_modules && pnpm install) if node_modules is corrupted or hand-edited

Example fix

// before
importPeer('@sveltejs/vite-plugin-svelte/src/wrong-path');
// after
importPeer('@sveltejs/vite-plugin-svelte/src/index.js'); // verify against its package.json exports
Defensive patterns

Strategy: validation

Validate before calling

const pkg = require(`${pkgDir}/package.json`);
const exportExists = (name, sub) => {
  const e = pkg.exports?.[sub];
  return typeof e === 'string' || (e && ('import' in e || 'default' in e));
};

Type guard

const hasValidExport = (exports, sub) =>
  !!exports && [sub].some((s) => {
    const e = exports[s];
    return typeof e === 'string' || (e && typeof e === 'object' && ('import' in e || 'default' in e));
  });

Try / catch

try {
  const resolved = importPeer('@sveltejs/kit/src');
} catch (e) {
  if (e.message.includes('Could not find valid')) {
    console.error('Subpath not in package exports — check the installed version\'s exports map');
  } else throw e;
}

Prevention

When it happens

Trigger: Requesting a subpath export that the package doesn't declare in `exports`, a typo'd subpath (e.g. wrong casing or missing segment), or an outdated/incompatible version of the peer package whose exports map lacks the needed entry.

Common situations: Version mismatch: newer Kit expects an export the installed peer doesn't have; typos in subpath imports; packages with restrictive exports maps; manually editing node_modules.

Related errors


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