When utilizing the --watch flag, Node-sass will fail to function properly

Encountering an issue with the "--watch" or "-w" flag when running node-sass. After installing node-sass as a devDependency and setting up a script to compile SCSS code, everything runs smoothly without any flags. However, adding the "--watch" flag causes issues - the initial code doesn't compile and no file changes are detected. Extensive research has yielded no solutions so far. Below are the scripts for reference, and additional information can be provided if needed.

In package.json:

"scripts": {
    "compile:sass": "node-sass sass/main.scss css/style.css"
}
 

Executing npm run compile:sass successfully compiles the SCSS code:

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5aba4b1aab0b7b685f4ebf5ebf5">[email protected]</a> compile:sass /Users/lucabarcelos/WebstormProjects/AdvancedCSS/Natours
node-sass sass/main.scss css/style.css

Rendering Complete, saving .css file...
Wrote CSS to /Users/lucabarcelos/WebstormProjects/AdvancedCSS/Natours/css/style.css

However, updating the script like this:

"scripts": {
    "compile:sass": "node-sass sass/main.scss css/style.css -w"
}

Results in the following output:

<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5dbd4c1dac0c7c6f5849b859b85">[email protected]</a> compile:sass /Users/lucabarcelos/WebstormProjects/AdvancedCSS/Natours
node-sass sass/main.scss css/style.css -w

The process appears to be waiting for file changes, but upon making changes, nothing happens at all.

Answer №2

Encountered the same issue (while working on the natours project:)) and found that none of the suggested solutions resolved it for me. I experimented with an onchange file watcher but that didn't work either. For those facing a similar problem with watching .scss files, I decided to share my solution here.

What finally did the trick was using both chokidar and node-sass in combination (node-sass-chokidar). Following the steps outlined in this tutorial, I was able to set it up successfully. The only variations I made were utilizing the -D flag to save it as a devDependency and omitting the -o flag since the output destination file was already in place.

I installed:

npm i node-sass-chokidar -D

followed by

npm i autoprefixer chokidar-cli npm-run-all postcss-cli -D

My scripts

"scripts": {
    "build-task:scss-compile": "node-sass-chokidar --source-map true sass/main.scss  css/style.css",
    "build-task:autoprefixer": "postcss css/*.css --use autoprefixer -d css",
    "sass:build": "npm-run-all -p build-task:*",
    "sass:watch": "chokidar 'sass/**/*.scss' -c 'npm run sass:build'",
    "dev": "npm-run-all -p sass:*"
  }

You can then execute npm run dev or npm run sass:watch.

Answer №3

Here is how I solved the issue:

  1. First, I installed gaze using npm by running npm install gaze -D
  2. Next, I created a new JavaScript file and used gaze to monitor the sass files for any changes. To execute the desired commands, I utilized the exec method from the child_process library in nodejs.

In order to include this functionality in my project, I added a script to the scripts property in the package.json file.

"sass:compile": "node-sass [your-input-src] [your-output-dest]"

In my watch.js file:

const gaze = require("gaze");
const { exec } = require("child_process");

// Define the command to be executed in the terminal
const COMMAND = "npm run sass:compile";

// Function that takes a command and uses 'exec' to run it in the terminal (shell)
function cmd(command) {
  exec(command, (err, stdout, stderr) => {
    if (err) {
      console.log("error: ", err);
      return;
    }
    if (stderr) {
      console.log("stderr: ", stderr);
      return;
    }
    console.log("stdout: ", stdout);
  });
}

// Setting up gaze to watch for changes in sass files
gaze("sass/*.scss", (err, watcher) => {
  if (err) {
    console.log(err);
    return;
  }

  // List of watched files
  const files = watcher.watched();
  console.log("Watching these files: ", files);
  
  // Gaze emits four events (changed, added, deleted, all). Listen to the ones you want.
  watcher.on("changed", (filePath) => {
    console.log(filePath, "file changed.");
    cmd(COMMAND);
  });

  watcher.on("added", (filePath) => {
    console.log("File: ", filePath, "was added.");
  });

  watcher.on("deleted", (filePath) => {
    console.log(filePath, "was deleted.");
  });

  watcher.on("all", (event, filePath) => {
    console.log("All: ", filePath, " was ", event);
  });
});

For more information on the gaze library, refer to their documentation here: https://github.com/shama/gaze

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

Issue with NodeJS and MariaDB Connector's createConnection() method not returning the correct connection object

My Ubuntu 18/04 machine has Node.js installed in version: node -v v14.16.0 The following modules are currently installed: sudo npm list -g --depth=0 /usr/lib ├── <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c ...

Utilize the nodeclipse extension for enhanced functionality

Recently, I started using the Nodeclipse plugin in Eclipse to work with NodeJS and ExpressJS. However, when I created an ExpressJS project and tried to run the app.js file, I encountered the following error message: /home/ali/node_modules/express/lib/expr ...

What steps must be taken to establish a universal connection to MongoDB that can be shared across multiple files?

Currently, I have my main server file named index.js: const express = require('express') const app = express() const route = require('./route') app.use('/main', route) app.listen(3000) Additionally, there is the route.js f ...

Incorporating a subdirectory into a module

Currently, I am attempting to utilize the gtfs-realtime-bindings node module. Despite the fact that the version available on npm is outdated, I have opted to download it directly from GitHub. However, gtfs-realtime-bindings is an extensive repository with ...

Completes a form on a separate website which then populates information onto a different website

Creating a website that allows users to login and view various complaint forms from government websites or other sources. When a user clicks on a link to file a complaint, they will be redirected to the respective page. However, I am looking for a way to ...

Dynamically load WordPress content using ajax and incorporate CSS3 animations for added visual appeal

It's time for a new challenge to push my learning curve. I've never ventured into the world of ajax before, but it seems like a skill I need to acquire. What better opportunity to learn than by implementing it on a fresh portfolio site. The main ...

Is it possible to develop my code as an express application while using loopback for scaffolding my application?

I have created my application using loopback scaffolding, but I am facing challenges with their components due to incomplete documentation. Can I simply enable the desired functionality as I would with express framework? My main goal is to integrate OAuth ...

Ways to initiate a CSS transition without hovering over it

Currently, I have a sidebar that is hidden and only shows up when the mouse hovers over it. I'm wondering how I can make the CSS transition trigger before the mouse hover, similar to what happens in this sidebar link: . Any advice on how to achieve th ...

Having trouble connecting to the online redis database

I am currently working on establishing a connection to my online redis database. const redis = require('redis'); const client = redis.createClient({ password: '<password>', socket: { host: <host> port: <p ...

Progressively increasing the time by 15 seconds during each iteration of the CSS @each loop

I recently made changes to a CSS script that involves the repetition of an animation task. Rather than repeating a segment of code 30 times, I decided to streamline it using a loop. Now, there are 30 <div></div> segments and each one undergoes ...

Invoke the function once the database information has been retrieved

I am new to Node.js and I am attempting to execute a function after running a select query using the code below: private displayUserInfo(): any { let connect = this.connect(); connect.connect(function(err: any) { if (err) throw err; ...

Unable to delete the margin of Material-ui TextField

Having trouble removing the padding around material-ui's TextField component when using styled-components and inline styling. Any solutions to this issue? Interestingly, the inline width property is functioning correctly, unlike the styled-component w ...

Looking for Assistance in Grasping the Concept of ACL in Loopback

After defining my Model "ShippingAddresses," I implemented the following ACL rules: [ { "accessType": "*", "principalType": "ROLE", "principalId": "$everyone", "permission": "DENY" }, { "accessType": "READ", ...

Conceal the Standard Select Dropdown Arrow in Internet Explorer 9

VIEW DEMO I attempted to use the CSS property appearance: none; to hide the select dropdown arrow, but it still shows in IE9. Refer to the image for clarification. Does anyone have a solution to hide the arrow using either CSS or jQuery? Below is my cod ...

Is it possible to add a border to both the tbody and td

I currently have a table that is organized with tbody elements to group rows together. In order to create a grid-like structure, I applied borders to each individual td element within the tbody. However, I also desire to show that the tbodies themselves ar ...

Here's how you can combine two individual LESS files using grunt-recess

Hey there! I'm working on compiling 2 separate files using grunt recess: recess: { dist: { options: { compile: true }, files: { 'path/css/custom. ...

Can you provide guidance on utilizing the Login Event within the Electron Framework?

I need some assistance in understanding the functionality of the Event: 'login' feature within the Electron Framework. Is this similar to the Password Autofill/Remember Password feature typically found in web browsers? I'm interested in util ...

The Maven build encountered an error while trying to execute a goal, resulting in the inability to

I'm currently working on a Windows 7 computer and I've received some code that requires me to execute "mvn install" in order to build the application. However, when I try to run this command, I encounter the following error: Failed to execute ...

When accessing req.param on the server side in a Node.js application, it returns undefined

I am currently utilizing nodejs and angularjs for my project. In the client-side javascript file, I made a GET request to the nodejs server with necessary data in parameters. itinerary.js $http({ method : "GET", url : '/createItinerary&apo ...

What causes the disparity between Chrome's print preview and printed output? [HTML - CSS]

In my Angular demo project, I have included basic text and a table. There is a print button that calls window.print() to print the page with applied styling. printPage() { window.print(); } CSS: @media print { @page { size: landscap ...