keizah7's avatar
Level 17

Laravel sail BrowserSync

Just started using laravel sail and can't make browsersync work. It does'nt reload the browser

mix.browserSync({
        open: false,
        notify: false,
        proxy: 'http://iskelbimai.test',
        port:8084
    });

Any solutions?

0 likes
15 replies
tinkerbell's avatar

Hope it helps! it's working with me

mix
  .js("resources/js/app.js", "public/js")
  .sass("resources/sass/app.scss", "public/css")
  .browserSync({
    proxy: 'localhost', // your domain test
    port: 32816 // your port
  });
keizah7's avatar
Level 17

I will try it. Can you show your node setup in docker-compose.yml ?

1 like
leolll's avatar

On a fresh laravel project I can't even add mix.browserSync('localhost'); to my webpack file without getting error messages when I run sail npm run dev. I just get unable to resolve dependency tree errors.

Here are the errors in case others are interested:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! Found: [email protected]
npm ERR! node_modules/webpack
npm ERR!   peer webpack@">=2" from [email protected]
npm ERR!   node_modules/babel-loader
npm ERR!     babel-loader@"^8.0.4" from [email protected]
npm ERR!     node_modules/laravel-mix
npm ERR!       dev laravel-mix@"^5.0.1" from the root project
npm ERR!   peer webpack@"^3.0.0 || ^4.0.0" from [email protected]
npm ERR!   node_modules/extract-text-webpack-plugin
npm ERR!     extract-text-webpack-plugin@"v4.0.0-beta.0" from [email protected]
npm ERR!     node_modules/laravel-mix
npm ERR!       dev laravel-mix@"^5.0.1" from the root project
npm ERR!   4 more (file-loader, friendly-errors-webpack-plugin, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer webpack@"^4.0.0" from [email protected]
npm ERR! node_modules/css-loader
npm ERR!   css-loader@"^1.0.1" from [email protected]
npm ERR!   node_modules/laravel-mix
npm ERR!     dev laravel-mix@"^5.0.1" from the root project
npm ERR!   peer css-loader@"*" from [email protected]
npm ERR!   node_modules/vue-loader
npm ERR!     vue-loader@"^15.4.2" from [email protected]
npm ERR!     node_modules/laravel-mix
npm ERR!       dev laravel-mix@"^5.0.1" from the root project
smoetje's avatar

Had the same issue. Got that one working properly in laravel sail without errors, by running the following:

npm run watch --legacy-peer-deps

Doing an nmap on the server addres hosting the projext, it appears only ports 445, 1025, 80, 22, 139 and 3306 are opened. So with laravel sail I am currently also unable to use browsersync and connect through the default ports (3000 /3001).

smoetje's avatar
smoetje
Best Answer
Level 3

I initially ran npm inside the terminal (WSL2/Ubuntu). For installation npm install works just fine. But npm run watch always resulted in an "Unhandled error event", "Error: listen ENOTSUP: operation not supported on socket".

I am working with Windows 10, Docker (WSL2 in an Ubuntu environment).

After searching I finally managed to get browserSync properly working with following config:

.browserSync({
	host: '127.0.0.1',
	proxy: 'localhost',
	open: false,
	files: [
            'app/**/*.php',
            'resources/views/**/*.php',
            'packages/mixdinternet/frontend/src/**/*.php',
            'public/js/**/*.js',
            'public/css/**/*.css'
	],
	watchOptions: {
            usePolling: true,
            interval: 500
}
});

Install Node & NPM (Windows version). Reboot, open Windows PowerShell and make sure you can access npm -v and node -v in your project path. There you can run: npm run watch

Make sure your docker-project is up in the WSL2 terminal and over there run: sail up

Then open your browser, go to http://localhost:3000 and you're all set to go!

If you happen to have errors in PowerShell in the process, just delete the /node_modules folder completely. Reinstall it with npm install inside PowerShell instead and then try again. When the folder structure was downloaded earlier inside another environment (e.g. linux) and you use e.g. PowerShell now, I noticed that's commonly a cause. Reinstalling fixes the issue.

7 likes
keizah7's avatar
Level 17

I am using mac os, but your setup for browserSync works for me too. Thanks!

After sail update my old code also works :)

Daniel-Pablo's avatar

@smoetje thanks a lot, following the steps I was able to really work with bowersync insinde SAIL working with the docker container notice that also I add this to the docker compose file as metioned below this chat

services:
    laravel.test:
        build:
            context: ./docker/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
            - 3000:3000
            - 3001:3001
5 likes
james0r's avatar

@Arius Tigger Just mapping the ports worked for me using Sail on macOS. Thanks!

2 likes
jim01's avatar

@Arius Tigger Adding the ports worked for me, macOS and Sail - thanks!

Pjgeeroms's avatar

Following these steps should get it working:

  • Add browsersync to your mix config
mix.js('resources/js/app.js', 'public/js')
    .postCss('resources/css/app.css', 'public/css', [
        require('tailwindcss'),
    ])
    .browserSync('localhost');
  • Open the docker-compose.yml in the root of your project, under the laravel.test service you want to add the port 3000 (this is the default port for browsersync). Your ports config should look like this:
ports:
  - '${APP_PORT:-80}:80'
  - 3000:3000
  • Run laravel mix inside your container: sail npm run watch

Following these steps you should now be able to visit localhost:3000 and be able to see your app. Changing stuff should trigger the reload.

10 likes
digitlimit's avatar

Ensure you add the port in your docker-compose.yml

You will which the ports to add when you run sail npm watch

[Browsersync] Proxying: http://laravel.test
[Browsersync] Access URLs:
 ------------------------------------
       Local: http://localhost:3000
    External: http://172.23.0.10:3000
 ------------------------------------
          UI: http://localhost:3001
 UI External: http://localhost:3001

This means we need to tell docker to expose port 3000 and port 3001(optional)

So in the docker-compose.yml file.

services:
    laravel.test:
        build:
            context: ./docker/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
            - 3000:3000
            - 3001:3001

NB: if you cannot find your docker-compose.yml file, run php artisan sail:publish

Then visit:

http://laravel.test:3000

5 likes

Please or to participate in this conversation.