composer/composer · error · InvalidArgumentException
Could not find package "{needle}" in your project
Error message
Could not find package "{needle}" in your project What it means
Thrown by BaseDependencyCommand (why/why-not) when findPackagesWithReplacersAndProviders() returns no packages matching the requested needle in the installed/locked repositories. The needle (first argument) must correspond to an installed package or one that is provided/replaced by an installed package. It signals the dependency is not present in the project.
Source
Thrown at src/Composer/Command/BaseDependencyCommand.php:102
return 1;
}
$repos[] = $localRepo;
$platformOverrides = $composer->getConfig()->get('platform') ?: [];
$repos[] = new PlatformRepository([], $platformOverrides);
}
$installedRepo = new InstalledRepository($repos);
// Parse package name and constraint
$needle = $input->getArgument(self::ARGUMENT_PACKAGE);
$textConstraint = $input->hasArgument(self::ARGUMENT_CONSTRAINT) ? $input->getArgument(self::ARGUMENT_CONSTRAINT) : '*';
// Find packages that are or provide the requested package first
$packages = $installedRepo->findPackagesWithReplacersAndProviders($needle);
if (empty($packages)) {
throw new \InvalidArgumentException(sprintf('Could not find package "%s" in your project', $needle));
}
// If the version we ask for is not installed then we need to locate it in remote repos and add it.
// This is needed for why-not to resolve conflicts from an uninstalled version against installed packages.
$matchedPackage = $installedRepo->findPackage($needle, $textConstraint);
if (!$matchedPackage) {
$defaultRepos = new CompositeRepository(RepositoryFactory::defaultRepos($this->getIO(), $composer->getConfig(), $composer->getRepositoryManager()));
if ($match = $defaultRepos->findPackage($needle, $textConstraint)) {
$installedRepo->addRepository(new InstalledArrayRepository([clone $match]));
} elseif (PlatformRepository::isPlatformPackage($needle)) {
$parser = new VersionParser();
$constraint = $parser->parseConstraints($textConstraint);
if ($constraint->getLowerBound() !== Bound::zero()) {
$tempPlatformPkg = new Package($needle, $constraint->getLowerBound()->getVersion(), $constraint->getLowerBound()->getVersion());
$installedRepo->addRepository(new InstalledArrayRepository([$tempPlatformPkg]));
}
} else {
$this->getIO()->writeError('<error>Package "'.$needle.'" could not be found with constraint "'.$textConstraint.'", results below will most likely be incomplete.</error>');View on GitHub (pinned to c435d285c9)
Solutions
- Verify the exact package name with `composer show` (lists installed packages) or `composer show --installed`.
- If the package is provided/replaced under another name, query that name instead.
- Run `composer install`/`composer update` so the package is present in the local repo.
- Check for typos against the package's canonical vendor/name on packagist.org.
Example fix
// before composer why monolog // after composer why monolog/monolog
Defensive patterns
Strategy: validation
Validate before calling
// Confirm the package is installed before running why/why-not:
exec('composer show --installed 2>&1', $out, $code);
if ($code !== 0 || !preg_grep('/^' . preg_quote($needle, '/') . '\s/', $out)) {
fwrite(STDERR, "$needle is not installed in this project\n");
exit(1);
} Prevention
- Run `composer show` to confirm exact installed package names before why/why-not.
- Remember why only searches local/locked repos, not all of packagist.
When it happens
Trigger: Running `composer why nonexistent/pkg`, misspelling a package name (`composer why monolog`), or querying a package that exists in the lock but under a different name (e.g. a provide/replace alias).
Common situations: Typo in package name, querying a transitive dependency by the wrong name, or running why before install so the package isn't in the installed repos. The message says "in your project" because it only searches local/locked repos, not all of packagist.
Related errors
- A valid composer.lock file is required to run this command w
- Package "${name}" is installed but not a direct dependent of
- Not enough arguments (missing: "packages").
- Invalid working directory specified, {workingDir} does not e
- --abandoned must be one of {values}.
AI-assisted analysis of composer/composer@c435d285c9 (2026-08-07).
Data as JSON: /api/errors/bfdeacf8fae96e06.
Report an issue: GitHub.