Laravel 5.5生产环境报错“It is unsafe to run Dusk in production”解决方案
Hey there, let's work through this Dusk production error you're facing. I know you've already tried troubleshooting, so let's dive into targeted fixes that might resolve the issue:
1. Ensure Dusk is only installed as a dev dependency
First, double-check your composer.json file—Laravel Dusk should live under the require-dev section, not require. This ensures it never gets installed in production environments.
After confirming, run this command on your production server to install only production dependencies:
composer install --no-dev
This removes Dusk entirely from your production setup, eliminating the root cause of the error.
2. Fix the AppServiceProvider registration logic
It looks like your registration code was cut off—make sure it's fully implemented with proper namespacing and environment checks. Here's the complete version:
public function register() { // Only register Dusk in local or testing environments if ($this->app->environment(['local', 'testing'])) { $this->app->register(\Laravel\Dusk\DuskServiceProvider::class); } }
Also, verify your production server's APP_ENV is correctly set to production (check your .env file or server environment variables—sometimes server configs can override .env values).
3. Remove Dusk from global provider registration
Check your config/app.php file's providers array. If Laravel\Dusk\DuskServiceProvider::class is listed there, remove it immediately. Global provider registration ignores environment checks and will load Dusk everywhere.
4. Clear cached configurations on production
Cached configs can sometimes cause environment checks to fail. Run these commands on your production server to clear all caches:
php artisan config:clear php artisan cache:clear php artisan route:clear php artisan view:clear
Start with step 1 (using --no-dev during composer install) since it's the most reliable way to prevent Dusk from being present in production at all. If that alone doesn't fix it, work through the other steps to eliminate any accidental registration or cached issues.
内容的提问来源于stack exchange,提问作者Samira kalantari

