laravel/framework · critical · RuntimeException
Unable to detect application namespace.
Error message
Unable to detect application namespace.
What it means
Thrown by Application::getNamespace() when it cannot infer the application namespace from composer.json. The method reads basePath/composer.json, walks autoload.psr-4 entries, and matches one whose realpath equals the app/ directory (Application::path()). If composer.json is unreadable/malformed or no PSR-4 entry maps to app/, detection fails. This typically breaks bootstrapping or any 'make:' command that needs the root namespace (e.g. make:controller, make:model).
Source
Thrown at src/Illuminate/Foundation/Application.php:1741
* @throws \RuntimeException
*/
public function getNamespace()
{
if (! is_null($this->namespace)) {
return $this->namespace;
}
$composer = json_decode(file_get_contents($this->basePath('composer.json')), true);
foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) {
foreach ((array) $path as $pathChoice) {
if (realpath($this->path()) === realpath($this->basePath($pathChoice))) {
return $this->namespace = $namespace;
}
}
}
throw new RuntimeException('Unable to detect application namespace.');
}
}
View on GitHub (pinned to bd6b5437e6)
Solutions
- Ensure composer.json at the project root contains "autoload": { "psr-4": { "App\\": "app/" } } matching your app/ directory.
- Run `composer dump-autoload` after any composer.json autoload change.
- Confirm the app/ directory exists relative to basePath and realpath() resolves it (no broken symlinks / open_basedir restrictions).
- Verify basePath is set correctly in bootstrap/app.php and you are running Artisan from the project root.
- If you renamed the app directory, update the PSR-4 path value to match (e.g. "App\\": "src/").
Example fix
// before — composer.json missing/incorrect autoload, `php artisan make:controller Foo` throws
// after — composer.json
{
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}
// then run:
// composer dump-autoload Defensive patterns
Strategy: validation
Validate before calling
$composerPath = base_path('composer.json');
if (! file_exists($composerPath)) {
throw new \RuntimeException('Missing composer.json at: '.$composerPath);
}
$autoload = json_decode(file_get_contents($composerPath), true)['autoload']['psr-4'] ?? [];
$appPath = realpath(app_path());
$ok = false;
foreach ($autoload as $namespace => $paths) {
foreach ((array) $paths as $p) {
if (realpath(base_path($p)) === $appPath) { $ok = true; break 2; }
}
}
if (! $ok) {
throw new \RuntimeException('composer.json PSR-4 does not map to app/ — namespace detection will fail.');
} Type guard
// Namespace detection is a runtime string operation, not a type; guard with the pre-check above before invoking make:* or getNamespace().
Try / catch
try {
$namespace = app()->getNamespace();
} catch (\RuntimeException $e) {
if (str_contains($e->getMessage(), 'Unable to detect application namespace')) {
// run composer dump-autoload or fix PSR-4 mapping
}
throw $e;
} Prevention
- Keep "App\\": "app/" in composer.json PSR-4 and run composer dump-autoload after changes.
- Run artisan from the project root; verify basePath in bootstrap/app.php.
- Add a CI step asserting composer.json has a PSR-4 entry matching app/.
When it happens
Trigger: Running artisan make:* commands, anything calling app_path() or $app->getNamespace(), or bootstrapping a custom Laravel app where composer.json was renamed, moved, or stripped of PSR-4 autoload. Also triggered by incorrect basePath (e.g. running Artisan from a subdirectory or a framework test harness without a proper composer.json).
Common situations: Renamed the app/ directory without updating composer.json. Set a custom namespace in composer.json but forgot to run composer dump-autoload. Copied framework src/ without a project composer.json. Bootstrap path/basePath misconfigured. Using a non-standard skeleton that omits autoload.psr-4.
Related errors
- Please install the "spatie/fork" Composer package in order t
- A container implementation is required to use the scheduler.
- To enable support for closure jobs, please install the illum
- Unable to resolve the dispatcher from the service container.
- Unsupported cipher or incorrect key length. Supported cipher
AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06).
Data as JSON: /data/errors/d2fae20496c08649.json.
Report an issue: GitHub.