Skip to content
Latchkey

Laravel "Class Faker\... not found" in factories in CI

Model factories call Faker to generate data. fakerphp/faker ships as a dev dependency, so a composer install --no-dev in the test job removes it and any factory usage fails with "Class Faker\Factory not found." Install dev deps for tests.

What this error means

Seeding or a test that uses a factory fails with "Error: Class \"Faker\Factory\" not found" or "Class \"Faker\Generator\" not found", though it works locally.

phpunit
  Error: Class "Faker\Factory" not found

  in /vendor/laravel/framework/.../FakerConcern.php

Common causes

Installed with --no-dev, removing Faker

Faker is under require-dev. Installing without dev dependencies leaves factories unable to resolve the Faker classes.

A production-optimized vendor cache in the test job

Restoring a --no-dev vendor directory into the test job omits Faker entirely.

How to fix it

Install dev dependencies in the test job

  1. Run composer install without --no-dev.
  2. Confirm fakerphp/faker appears in the installed packages.
  3. Re-run so factories can call Faker.
Terminal
composer install --no-interaction --prefer-dist

Add Faker explicitly if it is missing

If require-dev lost the package, add it back so factories resolve.

Terminal
composer require fakerphp/faker --dev

How to prevent it

  • Never use --no-dev in the job that runs factories or tests.
  • Keep fakerphp/faker in require-dev.
  • Do not reuse a production vendor cache for tests.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →