Activate Gulp watch to run task just one time

I have integrated gulp-livereload to automatically refresh pages after modifying .js files.

Below is the code snippet:

const gulp = require('gulp');
const livereload = require('gulp-livereload');


gulp.task('jsLiveReload', () => {
  gulp
    .src('/home/ksdmwms/Desktop/projs/others/tests/gulp_test/src/js/file.js')
    .pipe(livereload());
});


gulp.task('watchJsTest', function() {
    livereload.listen({
        reloadPage: 'http://localhost/projs/others/tests/gulp_test/src/index.html'
    });
    gulp.watch('/home/ksdmwms/Desktop/projs/others/tests/gulp_test/src/js/file.js', gulp.series('jsLiveReload'));
});

The setup watches for changes in file.js.

Upon execution, the following output is generated:

gulp watchJsTest
[14:05:40] Using gulpfile /opt/lampp/htdocs/projs/others/tests/gulp_test/gulpfile.js
[14:05:40] Starting 'watchJsTest'...
[14:05:46] Starting 'jsLiveReload'...
[14:05:46] /home/ksdmwms/Desktop/projs/others/tests/gulp_test/src/js/file.js reloaded.

Currently, it only triggers a reload on the first save. Subsequent modifications do not trigger a reload.

How can I address this issue?

Just a reminder: I am utilizing the livereload chrome extension on Ubuntu 18.04.

Answer №1

Assuming gulp is located in

/home/ksdmwms/Desktop/projs/others/tests/gulp_test

You can test the following code:

var gulp = require('gulp'),
    livereload = require('gulp-livereload');


gulp.task('jsLiveReload', function() {
  gulp.src('./src/js/**/*.js')
    .pipe(livereload());
  //.pipe(gulp.dest('')); 
});


gulp.task('watchJsTest', function() {
    livereload.listen({
        reloadPage: 'http://localhost/'
    });
    gulp.watch('./src/js/**/*.js', ['jsLiveReload']);
});

Please let me know if it works :) Have you given a shot to brwosersync?

Answer №2

Browser-sync vs Gulp-live-reload

Check out Browser-Sync

const gulp = require('gulp')
const browserSync = require('browser-sync').create()

gulp.task('js', () => {
  return gulp.src([
      'Files1',
      'File2'
    ])
    .pipe(gulp.dest(''))
    .pipe(browserSync.stream())
})

gulp.task('serve', ['sass', 'js'], () => {
  browserSync.init({
    server: './src',
  })

  gulp.watch([
    'src/sass/*.scss',
    'src/js/*.js',
  ], ['sass', 'js'])

  gulp.watch('src/*.html').on('change', browserSync.reload)
})

gulp.task('default', ['serve'])

When running the serve task, keep an eye on gulp.watch.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript error in the Electron browser is causing a glitch

I am a beginner in the world of Node.js, JavaScript, and Electron. My goal is to create a basic application that can open a local HTML file in a browser window. The local file contains some complex embedded JavaScript (TiddlyWiki). Below is a snippet of sa ...

Guide on how to update a div element without losing submitted information with the help of AJAX and PHP

After creating two HTML pages that incorporate AJAX refresh div tag code and use $_POST to post data from one page to another, I encountered a situation where the PHP page would retrieve data from a MySQL table. Let's take a look at the code I utilize ...

The PurgeCSS CLI does not generate CSS files beyond the command line interface

I'm struggling to successfully extract my CSS using purgecss from the command line and save it to a separate file. The instructions on PurgeCSS.com are vague: By default, the CLI only outputs the result in the console. To save the purified CSS files ...

Is it possible to animate captions and slides individually within a Bootstrap 5 carousel?

Beginner coder here, testing my skills with a self-assigned task. I've created a carousel using Bootstrap 5 with three slides, each containing two lines of captions. As the slides transition, the captions move in opposite directions—up and down. Ho ...

What is the process for sending a json response from a function?

Currently, I am experimenting with Javascript, node js, and sails. I am in the process of learning and figuring out how to accomplish certain tasks. At the moment, I have a simple example that I am working on. My goal is to fetch a json using a rest call ...

Tips on how to render a component only after receiving an AJAX response

I'm encountering an issue with one of my React components. It seems like AJAX is not fetching all the content from the external server before React renders the ChildComp component. https://i.stack.imgur.com/o0ZtH.png Above, you can view the tree of ...

Using a carousel component in Bootstrap

Just starting out with this, trying to customize Bootstrap to change slides automatically. I followed the documentation at https://getbootstrap.com/docs/4.3/components/carousel/ but for some reason, the slides aren't changing on an interval, even thou ...

Understanding the reverse order of numbers displayed with while loop and innerHTML

function doItAgain() { var loopCount = 5; while(loopCount > 0) { var target = document.getElementById("target"); target.innerHTML = "LoopCount: " + loopCount + "& ...

I'm looking for a way to incorporate JavaScript code within an iframe tag. Specifically, I'm attempting to embed code into a Wix website using the "Embed HTML

Trying to execute the code below on a Wix website using "Embed HTML", but IFRAME is blocking scripts. Seeking help to embed custom HTML with JavaScript on platforms like Wix.com or Wordpress.com, as the embedded code is not functioning due to IFRAME restri ...

Internet Explorer does not return results when using AJAX during the onchange event (specifically for IE only

My code is functioning correctly on other browsers, however in IE it does not provide any result when I select the dropdown button. Instead, it changes and displays an empty result. This is my AJAX: $("#book").change(function(){ var DOMBULK = $(" ...

What is a callback function tied to a specific URL?

I need to implement a functionality in my web application where users can click on a link within a datatable, which will then load a new table on a separate page. This new table should only display rows that have the same id as the row that was clicked on ...

What characteristics do you use to distinguish a non-compiled language?

Curiosity strikes me as I notice a common theme of "personal preference" regarding the distinctions between "Scripting Language" and "Programming Language". Here is my query: Is there a specific technical term for a language that operates without the nee ...

The async/await feature is not pausing for the completion of the async.map function call

I'm encountering an issue in my Node.js app where I need to gather and format data using a helper function for an API endpoint. The problem arises when trying to loop through an array and make asynchronous calls to the database for each entry. Despite ...

What is the solution for addressing 42 vulnerabilities: 3 considered low severity, 15 moderate, and 24 deemed as high

My Configuration Details: Environment and Versions: nodeJS: v14.18.1 npm: 8.1.1 expo-cli: 4.12.10 OS: MacOS Big Sur 11.6 Contents of my package.json: { "main": "node_modules/expo/AppEntry.js", "scripts": { "star ...

Nothing happens when the render_template method receives AJAX content in Flask with Python and JavaScript

When a certain condition is met, I am using ajax to pass data to my python function: if (lesson.length === 0) { $.ajax( { type:'POST', contentType:'application/json& ...

The module cannot be located, despite my efforts to reinstall and verify all addresses, the error still persists

Error in showStudent component: Module not located: Unable to resolve '@material-ui/data-grid' in 'C:\Users\USER\Desktop\Database\client\src\components\showStudent' The code in the showstudent ...

Guide to importing firebase-functions and firebase-admin using ES6 syntax for transpilation with Babel in Node 10

I've been working on my cloud functions in ES6 and using Babel to transpile them for the Node v10 environment. But, I've come across an odd issue. It seems that when I import firebase-functions like this: import functions from 'firebase-fu ...

Utilizing multiple materials with a single mesh in three.js: A comprehensive guide

I am facing a major issue with three.js: My goal is to create a simple cube with different colors on each face. I attempted to achieve this using the following code snippet: // set the scene size var WIDTH = jQuery('#showcase').width() - 20 ...

How can global variables be effectively shared and edited between two separate runs of JavaScript nodes?

The primary function contained in main.js is as follows: var nLastPingTime = 0, nLastPingNumber = 0; module.exports = { compareData: function(nPingTime, nLastPingNumber){ nLastPingTime = nPingTime; nLastPingNumber = nLastPi ...

Validate with JavaScript, then display and submit

I've created a submit function to verify form inputs, and then, if desired (via checkbox), print as part of the submission process. The issue is that when printing, the form submission never finishes. However, without printing, the form submits corre ...