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

No results returned when listing resource groups using Azure's Node.js SDK

I am attempting to utilize the node SDK in order to enumerate resource groups. I was able to successfully do so with Interactive Login, but I'm encountering issues when using a service principal. const { ClientSecretCredential, DefaultAzureCredent ...

Should we assume that all dependencies listed in a React-webpack web app's package.json file are solely intended for development purposes?

While organizing my package.json file for a React-webpack web app (client side only, no API server), I realized that all of the dependencies are classified as dev dependencies. This is because in production, the React web app is not run - it is built and t ...

What is the best way to update the content of a div on an HTML page by incorporating the content of a div from a different page using JavaScript?

I have a requirement where I need to implement a link in a Table of Contents that, upon clicking, should replace the existing content of one div on a page with the content of another div on a different page. My goal is to accomplish this using only JavaScr ...

Using Javascript to automatically submit a form when the enter key is pressed

Can anyone help me with a password form issue? I want the enter key to trigger the submit button but it's not working for me. I understand that the password can be viewed in the source code, this is just for practice purposes. <!DOCTYPE html> & ...

Cypress triggers timeout error due to oversized response

Issue Encountering a timeout error with Cypress when making a GET request. Description Using Cypress.io to send a GET request. Expecting a large response body (over 15Mb) from the API, but instead receiving an error message: "CypressError: cy.request( ...

Switching icon images using JQuery based on state changes

I'm attempting to switch the background image of my webpage's "body" element when the user toggles between playing and pausing music icons. This is what the CSS for the "body" element looks like: body { background: url('https://s3-us-wes ...

jQuery Mobile panel buttons are functioning properly within a maximum width of 300 pixels

I have constructed a panel that adjusts its size based on the device's screen using responsive design techniques. The code for this panel is as follows: <div data-role="panel" data-position="right" data-display="overlay" data-theme="a" id="add-for ...

Unlocking the Power of Node.js in Complex Business Scenarios

While I understand that this question may be ambiguous, I am seeking answers nonetheless. I am in the process of setting up a JSON-based WebService to provide information to multiple websites and mobile apps. Given that we frequently run CPU intensive My ...

It is not possible to decrease the size of the image that is currently used as the background image

Three images are arranged in this manner: https://i.stack.imgur.com/u4PdK.png The HTML code for this setup is as follows: <div class="lb-controlContainer"> <div class="lb-closeContainer"> <a class="lb-close"&g ...

Unable to adjust table, row, and cell heights in Outlook template due to HTML/CSS formatting issues

I'm struggling to make the height of the leftmost (commented) nested table adjust automatically based on the content in the right section. It displays correctly in Chrome, but does not stretch in Word/Outlook. Any ideas on how to fix this for Word/Ou ...

Symbol '%' is not supported in Internet Explorer

My experience with IE versions 8 and 9 has been that they do not recognize the &percnt; entity. I have tested this on two different computers. I found information suggesting that it should be supported in IE here: http://code.google.com/p/doctype/wiki ...

Assistance needed with WebSocket and Node.js: Unable to connect to wss://domain:8080 - Issue is not specific to any browser

Greetings, this is my initial inquiry on stack overflow so I'll give it my best shot. Despite going through numerous related questions, I haven't found the solution to my issue. I recently delved into Node.js because I required websockets for ce ...

The view is missing the stylesheet and it failed to load

Currently, I have a CSS file residing within the CSS folder of the public directory. app.set('view engine', 'ejs'); app.use(express.static('public')); In my index.ejs file, I am referencing the CSS as follows: <lin ...

Progressing through the Material UI LinearProgress bar in steps

How can I split a progress bar into more steps to achieve a design like this? https://i.stack.imgur.com/lRMj6.png I attempted using this code but couldn't find an option for splitting: <LinearProgress variant="determinate" classes={{ ...

What is the process for mocking the cookie method using jest and superTest?

While working on my application, I needed to set up test mocks for the users endpoint. To achieve this, I created a middleware function that returns a user cookie. My question now is how can I implement this using jest? Below is the code snippet from my l ...

Able to install npm packages, but unable to execute them

Recently, I've encountered an issue with my node.js/npm setup. After installing it through the official website, everything seemed to be working properly. The installation prompted me to ensure that /usr/local/bin/npm is included in my path, and upon ...

What is the best way to display numerical data within an inline list format?

Here is a list I have: <ol> <li>Login</li> <li>Address</li> <li>Shipping</li> </ol> The numbers in the list display correctly as decimals. However, when I change the <li> tag to inline or ...

Creating a responsive layout with two nested div elements inside a parent div, all styled

I'm currently facing an issue with using padding in my CSS to make the two divs inside a parent div responsive at 50% each. However, when combined, they exceed 100%. I suspect this is due to the padding, but I'm unsure how to resolve it. .column ...

Does the Loopback Model have an "exists" method that includes a where clause?

I am interested in querying the data using filters to check for existence. Does loopback support this method of querying? If so, could you provide some guidance? myModel.exists({where: {and: [{city: "London"}, {gender: "F"}]}}, function(err, bool){ if(bo ...

Eliminating the glow effect, border, and both vertical and horizontal scrollbars from a textarea

Dealing with the textarea element has been a struggle for me. Despite adding decorations, I am still facing issues with it. The glow and border just won't disappear, which is quite frustrating. Could it be because of the form-control class? When I rem ...