Framework: production.ERROR: No application encryption key has been specified.

Created on 6 Oct 2018  路  3Comments  路  Source: laravel/framework

Hello!

I get this error

production.ERROR: No application encryption key has been specified.

when I frequently (more than 3 times/second) send requests to the server using AJAX (axios exactly).

I wonder why it is production.ERROR, because in my .env it is APP_ENV=local. Obviously, my APP_KEY is present.

Full trace:

[2018-10-06 11:09:05] production.ERROR: No application encryption key has been specified. {"exception":"[object] (RuntimeException(code: 0): No application encryption key has been specified. at <>\\vendor\\laravel\\framework\\src\\Illuminate\\Encryption\\EncryptionServiceProvider.php:42)
[stacktrace]
#0 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Support\\helpers.php(1038): Illuminate\\Encryption\\EncryptionServiceProvider->Illuminate\\Encryption\\{closure}(NULL)
#1 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Encryption\\EncryptionServiceProvider.php(46): tap(NULL, Object(Closure))
#2 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Encryption\\EncryptionServiceProvider.php(24): Illuminate\\Encryption\\EncryptionServiceProvider->key(Array)
#3 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(749): Illuminate\\Encryption\\EncryptionServiceProvider->Illuminate\\Encryption\\{closure}(Object(Illuminate\\Foundation\\Application), Array)
#4 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(631): Illuminate\\Container\\Container->build(Object(Closure))
#5 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(586): Illuminate\\Container\\Container->resolve('encrypter', Array)
#6 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(732): Illuminate\\Container\\Container->make('encrypter', Array)
#7 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(885): Illuminate\\Foundation\\Application->make('encrypter')
#8 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(813): Illuminate\\Container\\Container->resolveClass(Object(ReflectionParameter))
#9 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(780): Illuminate\\Container\\Container->resolveDependencies(Array)
#10 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(631): Illuminate\\Container\\Container->build('App\\\\Http\\\\Middle...')
#11 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(586): Illuminate\\Container\\Container->resolve('App\\\\Http\\\\Middle...', Array)
#12 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Application.php(732): Illuminate\\Container\\Container->make('App\\\\Http\\\\Middle...', Array)
#13 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(215): Illuminate\\Foundation\\Application->make('App\\\\Http\\\\Middle...')
#14 <>\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php(189): Illuminate\\Foundation\\Http\\Kernel->terminateMiddleware(Object(Illuminate\\Http\\Request), Object(Illuminate\\Http\\JsonResponse))
#15 <>\\public\\index.php(60): Illuminate\\Foundation\\Http\\Kernel->terminate(Object(Illuminate\\Http\\Request), Object(Illuminate\\Http\\JsonResponse))
#16 {main}
"} 

Route, connected to the issue has GET method and auth middleware.

Responsible route's method resturns collection:

    $users = User::select(...)->get();
    return response($users);

This issue happens time-to-time, but I'm worried about getting it on product staging.

Most helpful comment

You should cache your config with php artisan config:cache so that this issue doesn't happen. As to why it does happen without config cache - it's a long story :)

All 3 comments

You should cache your config with php artisan config:cache so that this issue doesn't happen. As to why it does happen without config cache - it's a long story :)

I have some lines of code that reads from the .env file using the function env(). For instance,
'client_secret' => env('PASSWORD_CLIENT_SECRET'),
If I cache the .env file then, the function env() returns null. I would like to understand why I have to cache the .env file to avoid the error "No application encryption key has been specified", why the error ocurrs when the file is not cached and finally, why the env() function doesn't work when the .env file is cached.

Of course, I have setted up my APP_KEY in my .env file.

i think the overall issue here is that reading the .env file has problems when you have too many requests

caching the config is a solution to this

the problem with env helper returning null is because you are using it outside the config files, which the docs have a warning against fir this reason

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iivanov2 picture iivanov2  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

digirew picture digirew  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments