composer/composer · error · RuntimeException

File "{file}" cannot be found in the current directory

Error message

File "{file}" cannot be found in the current directory

What it means

Thrown by BaseConfigCommand::initialize() after it resolves the config file path and finds the file does not exist (and --global was not set, which would auto-create it). It indicates the composer.json (or the --file target) is missing from the current working directory. This prevents writing config to a nonexistent file.

Source

Thrown at src/Composer/Command/BaseConfigCommand.php:79

            ($configFile === 'composer.json' || $configFile === './composer.json')
            && !file_exists($configFile)
            && realpath(Platform::getCwd()) === realpath($this->config->get('home'))
        ) {
            file_put_contents($configFile, "{\n}\n");
        }

        $this->configFile = new JsonFile($configFile, null, $io);
        $this->configSource = new JsonConfigSource($this->configFile);

        // Initialize the global file if it's not there, ignoring any warnings or notices
        if ($input->getOption('global') && !$this->configFile->exists()) {
            touch($this->configFile->getPath());
            $this->configFile->write(['config' => new \ArrayObject]);
            Silencer::call('chmod', $this->configFile->getPath(), 0600);
        }

        if (!$this->configFile->exists()) {
            throw new \RuntimeException(sprintf('File "%s" cannot be found in the current directory', $configFile));
        }
    }

    /**
     * Get the local composer.json, global config.json, or the file passed by the user
     */
    protected function getComposerConfigFile(InputInterface $input, Config $config): string
    {
        return $input->getOption('global')
            ? ($config->get('home') . '/config.json')
            : ($input->getOption('file') ?? Factory::getComposerFile())
        ;
    }

    /**
     * Get the local auth.json or global auth.json, or if the user passed in a file to use,
     * the corresponding auth.json
     */

View on GitHub (pinned to c435d285c9)

Solutions

  1. Ensure you are in the directory containing composer.json (check with ls).
  2. If no composer.json exists, run `composer init` first.
  3. Correct the --file path or remove it to target the default composer.json.
  4. If targeting the global config, use --global instead of --file.

Example fix

// before (run from wrong dir)
composer config minimum-stability dev
// after
cd /path/to/project && composer config minimum-stability dev
Defensive patterns

Strategy: validation

Validate before calling

$file = $optFile ?? 'composer.json';
if (!is_file(getcwd() . '/' . $file)) {
    fwrite(STDERR, "$file not found in " . getcwd() . "\n");
    exit(1);
}

Prevention

When it happens

Trigger: Running `composer config foo bar` in a directory with no composer.json, running config with --file=missing.json, or running config from the wrong working directory (e.g. a parent folder).

Common situations: Wrong cwd (running from repo root when composer.json is in a subdirectory), deleted or never-created composer.json, or a typo in the --file path.

Related errors


AI-assisted analysis of composer/composer@c435d285c9 (2026-08-07). Data as JSON: /api/errors/5f5bcdb0236f8db3. Report an issue: GitHub.