fbpx

Laravel It is unsafe to run Dusk in production error doing composer update

About Running Laravel Dusk in Production

Laravel Dusk is a testing framework and should not be run on a production server. Laravel reads the .env file and then determines is the server is in production or local or testing. If it’s in production, composer update will fail if it detects Laravel Dusk. Here are some possible reasons why you might be getting this error.

Reasons

Incorrectly installed

When installing Dusk, the documentation states:

composer <span class="token keyword">require</span> <span class="token operator">--</span>dev laravel<span class="token operator">/</span>dusk

If you forgot the --dev flag, Dusk will end up in the wrong place in composer.json. Move it to the require-dev section in composer.json and do composer update

Composer on Production

Used composer install instead of composer install --no-dev

When installing composer on a production server, don’t just do composer install but do composer install --no-dev

Discovery

Probably the best way to avoid this problem is to add dont-discover in composer.json. You’ll have some interesting challenges if you’re using version control, but at least on your production server, until your next composer.json update, you’ll be fine.

1. So don’t discover the package in composer.json

"extra": {
   "laravel": {
      "dont-discover": [
         "laravel/dusk"
      ]
   }
},

2. Some people swear by the AppServiceProvider fix below, but in my experience it doesn’t work.

Check which environment exists before loading it in AppServiceProvider

<span class="kwd">public</span> <span class="kwd">function</span> <span class="kwd">register</span><span class="pun">()</span>
<span class="pun">{</span>
    <span class="com">// Dusk, if env is appropriate</span>
    <span class="kwd">if</span> <span class="pun">(</span><span class="pln">$this</span><span class="pun">-></span><span class="pln">app</span><span class="pun">-></span><span class="pln">environment</span><span class="pun">(</span><span class="str">'local'</span><span class="pun">,</span> <span class="str">'testing'</span><span class="pun">,</span> <span class="str">'production'</span><span class="pun">))</span> <span class="pun">{</span><span class="pln">
        $this</span><span class="pun">-></span><span class="pln">app</span><span class="pun">-></span><span class="kwd">register</span><span class="pun">(</span><span class="typ">DuskServiceProvider</span><span class="pun">::</span><span class="kwd">class</span><span class="pun">);</span>
    <span class="pun">}</span>
<span class="pun">}</span>

For solutions 1 and 2, since Dusk is a dev dependency, you can also do this when deploying:

<span class="pln">composer install </span><span class="pun">--</span><span class="kwd">no</span><span class="pun">-</span><span class="pln">dev</span>

3. Temporary change from production to local (Not recommended)

Although this is not recommended it can solve some high pressure situations. Key will be to remember to fix the problem long term.

References

https://quickadminpanel.com/blog/it-is-unsafe-to-run-dusk-in-production-what-to-do/
https://medium.com/@taylorotwell/package-auto-discovery-in-laravel-5-5-ea9e3ab20518
https://stackoverflow.com/questions/49622200/how-to-solve-exception-it-is-unsafe-to-run-dusk-in-production-in-laravel-5-5

 

 

Share this article

Share on facebook
Share on twitter
Share on linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top