Navigate back to the homepage

Refresh browser window whenever file changes are saved

Vishnu Damwala
November 20th, 2019 · 1 min read

A quick to configure auto-refreshing browser window whenever you save changes to files.

While working on a website containing HTML + CSS + core or vanilla JS, its quite tedious to every time refresh the page after saving changes to file. Many packages like React app, Gatsby, NextJS, Vue CLI, Svelte provides reloading the page when a file in the code editor is saved.

This is one of the best & most useful feature for developer, which saves your time and increases productivity.

Our search ended up with a npm package named browser-sync which reloads every time a file in the directory is saved, all the connected browsers will be reloaded.

Browsersync is an open-source project created and maintained by the front-end designers/developers at JH. Together with contributors from all over the world, we aim to make modern, multi-device web development faster & easier.

Install Node.js

Browsersync is a Node.js module so first you need to install it.

For Windows simply download the installer directly from the Node.js web site.

For Ubuntu execute below commands.

Node.js v12.x:

1curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
2sudo apt-get install -y nodejs

For other OS visit https://nodejs.org/en/download/package-manager

Install Browsersync

The Browsersync is available with Node.js package manager (npm). It can be installed globally with below command in terminal window so they’re available to all your projects.

1npm install -g browser-sync

Running Browsersync

Browsersync will provide a URL to view your site by starting a mini-server.

Navigate your terminal window to a your project directory and run the appropriate command

For static sites

1browser-sync start --server --files "."

For dynamic sites

If you’re already running a local server with PHP or similar, you’ll need to use the proxy mode. Browsersync will wrap your vhost with a proxy URL to view your site.

To view your site you’ll need to use the proxy mode with your already running local server such as PHP or any other. Your vhost will be wrapped with a proxy URL by Browsersync.

1browser-sync start --proxy "meshworld.dev" --files "."

Browsersync can be used to watch all files in the current directory (as well as sub-directories) for changes, and launches a web server on port 3000, opening a browser window to connect to the server.

Browser will be refresh with file changes

More articles from MeshWorld

What’s New in PHP 7.4

PHP 7.4 is coming with new features, deprecations, and a boost in performance. Find out the new PHP 7.4 features in this guide.

November 20th, 2019 · 2 min read

indexOf() Method in Javascript

JavaScript array indexOf() method returns the index of first occurrence at which a given element can be found in the array, or -1 if it is not present

October 18th, 2019 · 1 min read
© 2017–2020 MeshWorld
Link to $https://twitter.com/meshworldindiaLink to $https://facebook.com/meshworldindiaLink to $https://instagram.com/meshworld.india/