Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Fatal error: Class "Firebase\JWT\JWT" not found

ErrorFatal error: Uncaught Error: Class "Firebase\JWT\JWT" not found

Short answer

PHP cannot find the class because the package is not installed, the Composer autoloader is never loaded, or the file lacks a use statement. Run composer show firebase/php-jwt, require vendor/autoload.php once at the entry point, and import Firebase\JWT\JWT and Firebase\JWT\Key at the top of the file.

  • Applies to PHP 8.3, Composer 2, firebase/php-jwt 6
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask Composer whether the package is installed in this project, then check the vendor folder on the server that shows the error.

    composer show firebase/php-jwt
    ls vendor/firebase/php-jwt/src
  2. 2Match what you see, then jump to its fix

    1. 1composer show says the package is not found, or vendor/ is missing on the serverThe package is not installed on this machineFix
    2. 2The package is installed, yet every class from vendor/ is missingvendor/autoload.php is never loadedFix
    3. 3The message names a different class such as App\Controllers\JWT, or the code says new JWT or \JWTThe file has no use statement, or the namespace is wrongFix

What does the error look like?

  • Fatal error: Uncaught Error: Class "Firebase\JWT\JWT" not found
  • Class 'Firebase\JWT\JWT' not found

Cause 1 of 3

The package is not installed on this machine

1Check

A deploy that skips vendor/ (it is usually in .gitignore) leaves the server without the library.

composer show firebase/php-jwt

2Fix

Add the package, and on the server run install so it matches composer.lock.

composer require firebase/php-jwt
# on the server, after deploying composer.json and composer.lock
composer install

3Verify

composer show now lists the package and the folder exists.

ls vendor/firebase/php-jwt/src/JWT.php

Confirmed by github.com

Did this fix it?

Cause 2 of 3

vendor/autoload.php is never loaded

1Check

Composer generates vendor/autoload.php for libraries that provide autoload information, and the entry script must include it. Look for the require in your front controller.

grep -rn "vendor/autoload.php" index.php public/ src/ 2>/dev/null

2Fix

Require the autoloader once, before the first use of any package class, with a path relative to the file.

<?php
require __DIR__ . '/vendor/autoload.php';

3Verify

Reload the page; the error should change or disappear.

php -r 'require "vendor/autoload.php"; var_dump(class_exists("Firebase\\JWT\\JWT"));'

Confirmed by getcomposer.org

Did this fix it?

Cause 3 of 3

The file has no use statement, or the namespace is wrong

1Check

Inside a namespaced file an unqualified class name is looked up in the current namespace, and PHP does not fall back to the global one. Imports are per file, so each file needs its own.

grep -n "^use \|^namespace " src/AuthController.php

2Fix

Import both classes the library uses for decoding, and pass a Key object with the algorithm.

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$decoded = JWT::decode($token, new Key($secret, 'HS256'));

3Verify

Run the code path with a valid token; it returns the payload object.

php -l src/AuthController.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Commit composer.json and composer.lock, and run composer install in every deploy.
  • Keep one require of vendor/autoload.php in the entry point, not in each file.
  • Let your editor add use statements when you type a class name.
  • Pin the package version in composer.json so decode() usage stays stable.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

PHP says Class not found, but the file is there

Class not found, although the file is included

PHP finds a class only if the file defining it was already loaded or an autoloader can map the name to a file.

3 causes, each with a fix

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.