Steps for converting SCSS to CSS using node-sass

I have a main.scss file with numerous imports from other .scss files. Whenever I make changes to any of the *.scss files, the master.css file is automatically generated.

I rely solely on NPM for my build process and do not use Gulp or Grunt! It's important for me to keep it that way.

My current build script looks like this:

"scripts": {
  "watch-sass": "sass --watch src/scss/master.scss:dist/css/master.css"
}

While this works fine, the compilation process takes a significant amount of time!

Now, I'm exploring the option of using node-sass as it promises faster compilation speed. However, I'm having trouble grasping its usage completely... node-sass has been installed via npm install node-sass

Where should I integrate the following code (from the documentation)?

var sass = require('node-sass');
sass.render({
  file: scss_filename,
  [, options..]
}, function(err, result) { /*...*/ });
// OR
var result = sass.renderSync({
  data: scss_content
  [, options..]
});

This doesn't seem to be added directly to the package.json, right?

Here's a helpful tutorial I've come across: Using NPM as a Task Runner

The suggested script seems useful. How can I implement it?

"scripts": {
  "sass": "node-sass sass/ -o build/css/"
}

This command will compile all Sass files (excluding those starting with an underscore) into the build/css/ directory.

Your assistance in this matter would be greatly appreciated!

Answer №1

If you're looking for a solution to compile or convert sass / scss to css using node-sass without Ruby, this might help: How to compile or convert sass / scss to css with node-sass (no Ruby)?

Consider using grunt as an alternative for simpler and faster processing. The grunt plugin suggested in the tutorial is highly recommended: https://www.npmjs.com/package/grunt-contrib-sass

// UPDATE

Following the provided tutorial is straightforward. Create a "package.json" file in your root folder with the following content:

{
  "watches": {
    "sass": "sass/**"
  },
  "scripts": {
    "sass": "node-sass sass/ -o build/css/",
    "dev": "rerun-script"
  }
}

Open the command line in the root folder and execute the following commands:

npm install --save-dev node-sass

This will install node-sass.

npm install --save-dev rerun-script

This command sets up a watch task so you don't need to manually run node-sass every time you make changes.

Finally, run:

npm run dev

That's it! Your setup is now complete.

Answer №2

Check out the detailed documentation here.

To automatically compile files, make sure to include the flag -w in your command.

node-sass -w -r assets/src/scss/ -o assets/dist/css/

This is how my package.json file looks like:

{
  "name": "my-project",
  "version": "1.0.0",
  "scripts": {
    "build:js": "watchify assets/src/js/main_1.js -o 'exorcist assets/dist/js/main_1.js.map > assets/dist/js/main_1.js' -d -t [babelify --presets [latest]]",
    "build:scss": "node-sass -w -r assets/src/scss/ -o assets/dist/css/",
    "build": "npm run build:scss & npm run build:js"
  },
  "devDependencies": {
    "babel-cli": "^6.0.0",
    "babel-preset-latest": "^6.16.0",
    "babelify": "^7.3.0",
    "browserify": "^13.1.1",
    "exorcist": "^0.4.0",
    "node-sass": "^4.5.0",
    "watchify": "^3.7.0"
  },
  "browserify": {
    "transform": [
      "babelify"
    ]
  }
}

You can find the current version of my package.json here.

Answer №3

When working with the node-sass library, it's important to note that the `watch` mode does not initiate the first compilation by default. It assumes that you have already executed `node-sass` beforehand.

In my personal workflow, I typically set up something similar to the following:

{
  "scripts": {
    "sass": "node-sass -o /path/to/dist /path/to/src",
    "sass:watch": "npm run sass && npm run sass -- --watch --recursive"
  }
}

You can then utilize this setup by running: npm run sass: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

Divide a JSON API object into segments within an express application

One way I'd like to organize my API's output is by splitting it into multiple pages. My idea is to access them using URLs like this: http://127.0.0.1:3000/api/articles/0/<API-TOKEN> This specific URL would display the first page containing ...

What causes the significant variations in the `package-lock.json` files that are generated when I execute `npm install` on separate systems?

When I remove the node_modules and package-lock.json files from the same git repository, my assumption is that running npm install will result in the generation of the same package-lock.json file each time, with minor updates possible due to changes in mod ...

Having trouble getting BootstrapTour to work with meteor 1.4?

I've been trying to implement the flowkey:bootstrap-tour package for BootstrapTour in my meteor 1.4 app, but unfortunately it's not working as expected. Below are the code snippets I'm using: let tour = new Tour({ name: "tour", de ...

Resolving peer dependency conflict during npm installation

When attempting to run npm install @react-navigation/native @react-navigation/native-stack, I encountered the following errors: npm WARN ERESOLVE overriding peer dependency npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While ...

CSS effect() Practical duration

I have successfully implemented a CSS filter to blur an element, however, I am looking for a way to make the blur ease in smoothly. I'm not sure which css property controls this easing effect? Initially, I tried using transition: 1s; but that ended u ...

Managing nested request bodies in NestJS for POST operations

A client submits the following data to a REST endpoint: { "name":"Harry potter", "address":{ "street":"ABC Street", "pincode":"123", "geo":{ &q ...

Ways to display sneak peeks of preceding and succeeding images within a carousel using Bootstrap 5

I am looking to achieve a carousel effect where the previous and next images partially show on either side of the central image, similar to what is shown in this example: https://i.sstatic.net/HahbP.png Below is the code snippet. <div class="c ...

The content loses functionality once I add an overlay color to the background image div

I'm facing an issue with a div that has a background image, text, and a button in the center. Whenever I add an overlay color on top of the background image, the text and button seem to be disabled or unclickable. My goal is to ensure that the Read M ...

Unable to supersede CSS module styling using global or external stylesheets in React, Next.js, or React Native

I'm facing a challenge finding a solution to a basic issue. I have a component that is initially styled using a class from a *.module.css file. However, I want to replace this style with one from a global stylesheet or another stylesheet but I can&apo ...

Move the element from the center of the screen back to its starting position

Looking to craft a single animation that will transition elements from the center of the current view back to their original positions. Unfortunately, CSS animations do not support toggling the display property. This limitation causes the second rectangle ...

Ways to Safeguard Your API Key with HERE Map on the Client-Side

My current challenge involves concealing my API key from being visible in the network tab of a browser. Below are some GET requests I made to HERE Map, where the API key is exposed: https://1.base.maps.ls.hereapi.com/maptile/2.1/info?xnlp=CL_JSMv3.1.30.4&a ...

Terminating a client connection in Node.js using socket.io

What is the best way to terminate the socket connection on the client side? I am currently utilizing: socket.io 0.9 node.js 0.10.15 express 3.3.4 For example, when calling localhost/test -- server side var test = io .of('/test') .on(&apos ...

Transmitting information via Ajax, jquery, Node.js, and Express

Seeking assistance as I struggle to comprehend the process while trying to implement it based on various online resources. My carousel directs users right after signing up, and I aim to gather information about their profile through simple input questions. ...

How can one determine the template engine utilized by a website?

As I delve into reverse engineering a couple of web applications, my main objective is to uncover the technologies they are built upon. This will allow me to determine which technology stack to focus on learning in order to develop my own web application. ...

Tips for updating grid variables in Bootstrap 4 and Angular

What's the best way to truly override bootstrap variables? I've tried the following code but it's not working // overrides.scss @import '~bootstrap/scss/functions'; @import '~bootstrap/scss/variables'; @import '~boo ...

Guidelines for placing an HTML element in relation to another HTML element using CSS or JavaScript

Is there a way to position an HTML element in relation to another using CSS or JavaScript? I have attempted to copy the inner HTML of the "second-element" and append it inside the "first-element", but this approach is causing issues with other functional ...

The presence of ng-show dynamically adjusts the minimum height of a div element

I am encountering an issue with a div that has the class of wrapper. Inside this div, there is a parent div with the class of content-wrapper. The wrapper div includes a conditional directive ng-show which toggles between displaying or hiding its content. ...

Error in template loading

Currently, I am going through the Angular.js tutorial to learn how to incorporate some of its advanced features into my existing applications. Everything has been smooth sailing until I reached the (angular route) section. Unfortunately, I've hit a ro ...

What is the process for importing the util module in Node.js?

I attempted to use the isDeepStrictEqual() method for object comparison but encountered this error: util.isDeepStrictEqual() is not a function After checking the official documentation, I found out that this method was introduced in Node.js v9.0.0 w ...

Installation of Next.js from github using npm is unsuccessful

Encountering issues with npm installing zeit/nextjs directly from GitHub, leading to the following error message. The same error persists whether running in a Docker instance or on OSX, regardless of using Node 5.2 or 7.2. This is my first time installing ...