SASS/SCSS

Installing SASS & Setting Up Your Environment

15 min Lesson 2 of 30

Installing SASS & Setting Up Your Environment

Now that you understand what SASS is and why it's valuable, it's time to get hands-on! In this lesson, we'll walk through everything you need to install SASS, set up your development environment, and compile your first SCSS file to CSS.

Understanding Dart Sass: The Official Implementation

Before we install anything, let's clarify which version of SASS we're installing. As mentioned in the previous lesson, Dart Sass is the official, primary implementation of SASS maintained by the core team. It replaced Ruby Sass (deprecated in 2020) and is faster, more maintainable, and fully up-to-date with the latest SASS features.

Note: If you see references to "LibSass" or "Node-sass" in older tutorials, know that these are deprecated implementations. Always use Dart Sass for new projects.

Method 1: Installing SASS via npm (Recommended)

The most common and recommended way to install Dart Sass is through npm (Node Package Manager). This method works on Windows, macOS, and Linux.

Prerequisites

You'll need Node.js and npm installed on your system. Check if you have them:

Check Node.js and npm Installation

node --version
npm --version

If these commands return version numbers (like v18.16.0 for Node and 9.5.1 for npm), you're ready to proceed. If not, download and install Node.js from nodejs.org - npm comes bundled with it.

Global Installation

To install SASS globally (available for all projects on your system):

Install SASS Globally

npm install -g sass

After installation completes, verify it worked:

Verify SASS Installation

sass --version

You should see something like: 1.69.5 compiled with dart2js 3.2.3

Tip: Global installation is great for learning and quick experiments. For production projects, you'll typically install SASS as a project dependency (explained below).

Project-Specific Installation (Best Practice)

For real projects, install SASS as a development dependency in your project:

Install SASS as Project Dependency

# Navigate to your project directory
cd my-project

# Initialize npm if you haven't already
npm init -y

# Install SASS as a dev dependency
npm install --save-dev sass

This creates a node_modules folder and adds SASS to your package.json file under devDependencies. Now anyone who clones your project can install all dependencies (including SASS) with a simple npm install.

Your package.json Will Look Like This

{
  "name": "my-project",
  "version": "1.0.0",
  "devDependencies": {
    "sass": "^1.69.5"
  }
}

Method 2: Standalone CLI Installation

If you don't want to use npm, you can download the standalone Dart Sass executable directly:

  1. Visit the official GitHub releases page: github.com/sass/dart-sass/releases
  2. Download the appropriate version for your operating system (Windows, macOS, or Linux)
  3. Extract the archive
  4. Add the sass executable to your system PATH
Note: The standalone method works but requires manual updates. The npm method is generally easier and more maintainable.

Setting Up VS Code for SASS Development

Visual Studio Code is the most popular editor for web development, and it has excellent SASS support through extensions. Let's set it up for the best SASS experience.

Essential VS Code Extensions

1. Live Sass Compiler

This extension automatically compiles your SCSS files to CSS as you save them. It's incredibly convenient during development.

  • Open VS Code
  • Go to Extensions (Ctrl+Shift+X or Cmd+Shift+X)
  • Search for "Live Sass Compiler" by Glenn Marks
  • Click Install

Once installed, you'll see a "Watch Sass" button in the VS Code status bar. Clicking it will start watching your SCSS files for changes.

2. SCSS IntelliSense

This extension provides autocompletion, syntax highlighting, and code navigation for SCSS.

  • Search for "SCSS IntelliSense" by mrmlnc
  • Click Install
3. SCSS Formatter

Automatically formats your SCSS code for consistency.

  • Search for "SCSS Formatter" by Sibiraj
  • Click Install
Tip: Configure Live Sass Compiler to match your project needs. Create a .vscode/settings.json file in your project with custom compilation settings (we'll explore this later).

Your First SASS Compilation: The Command Line

Let's compile your first SCSS file using the command line. This teaches you the fundamentals before relying on automated tools.

Creating a Simple SCSS File

Create a new folder for your SASS project and add a file called styles.scss:

styles.scss

$primary-color: #3498db;

body {
  font-family: Arial, sans-serif;
  background-color: $primary-color;
  color: white;
  padding: 20px;

  h1 {
    font-size: 32px;
    margin-bottom: 10px;
  }
}

Compiling to CSS

Now let's compile this SCSS file to CSS using the command line:

Basic Compilation Command

sass styles.scss styles.css

This command tells SASS: "Take styles.scss as input and output styles.css."

After running this command, you'll see a new styles.css file created:

Generated styles.css

body {
  font-family: Arial, sans-serif;
  background-color: #3498db;
  color: white;
  padding: 20px;
}
body h1 {
  font-size: 32px;
  margin-bottom: 10px;
}

Notice how the variable was replaced with its value, and the nested h1 was compiled to body h1. That's SASS in action!

Note: You'll also see a styles.css.map file created. This is a source map file that helps browsers show you the original SCSS line numbers when debugging. We'll discuss source maps in detail shortly.

Watch Mode: Automatic Compilation

Manually running the compile command after every change is tedious. That's where watch mode comes in - SASS will automatically detect file changes and recompile.

Watch a Single File

sass --watch styles.scss:styles.css

Now SASS is watching styles.scss. Every time you save changes to that file, SASS automatically regenerates styles.css. You'll see output like:

Watch Mode Output

Sass is watching for changes. Press Ctrl-C to stop.

Compiled styles.scss to styles.css.

To stop watching, press Ctrl+C in the terminal.

Watching Entire Directories

In real projects, you'll have multiple SCSS files. Instead of watching them individually, watch entire directories:

Watch Directory

sass --watch scss/:css/

This tells SASS: "Watch all files in the scss/ folder and output compiled CSS to the css/ folder, maintaining the same folder structure."

For example:

  • scss/main.scsscss/main.css
  • scss/components/button.scsscss/components/button.css
Tip: Keep your source SCSS files in a scss/ or sass/ folder, and output compiled CSS to a css/ or dist/ folder. This keeps your project organized and makes it clear which files are source and which are generated.

Output Styles: Expanded vs Compressed

SASS can output CSS in different formats depending on your needs. The two main styles are "expanded" (human-readable) and "compressed" (minified for production).

Expanded Style (Default)

The expanded style is the default. It produces readable, well-formatted CSS:

Expanded Output

.button {
  background-color: #3498db;
  padding: 10px 20px;
  border: none;
}

.button:hover {
  background-color: #2980b9;
}

Compressed Style (Production)

Compressed style removes whitespace, comments, and unnecessary characters to minimize file size:

Compressed Output

.button{background-color:#3498db;padding:10px 20px;border:none}.button:hover{background-color:#2980b9}

To compile in compressed style:

Compile with Compressed Style

sass --style=compressed styles.scss styles.min.css
Tip: Use expanded style during development for easier debugging, and compressed style for production to reduce file size and improve page load times.

Source Maps: Debugging Compiled CSS

When your SCSS is compiled to CSS, the browser only sees the CSS file. If you inspect an element in DevTools, you'll see CSS line numbers, not the original SCSS line numbers. This makes debugging difficult.

Source maps solve this problem. They map the compiled CSS back to the original SCSS files, so DevTools shows you the SCSS filename and line number.

Source Maps Are Generated by Default

When you compile SCSS, SASS automatically generates a .map file:

Compilation with Source Maps

sass styles.scss styles.css

# This creates:
# - styles.css
# - styles.css.map

The CSS file includes a comment pointing to the source map:

Source Map Reference in CSS

/* CSS content here */

/*# sourceMappingURL=styles.css.map */

Disabling Source Maps

If you don't want source maps (e.g., in production), use the --no-source-map flag:

Compile Without Source Maps

sass --no-source-map styles.scss styles.css
Note: In production, some developers disable source maps to prevent exposing source code structure. Others keep them for easier debugging. It's a security vs. convenience trade-off.

Organizing Your SASS Project Structure

As your project grows, organization becomes critical. Here's a recommended folder structure for SASS projects:

Recommended Project Structure

my-project/
├── scss/
│   ├── base/
│   │   ├── _reset.scss
│   │   ├── _typography.scss
│   │   └── _variables.scss
│   ├── components/
│   │   ├── _buttons.scss
│   │   ├── _cards.scss
│   │   └── _navbar.scss
│   ├── layout/
│   │   ├── _header.scss
│   │   ├── _footer.scss
│   │   └── _grid.scss
│   ├── pages/
│   │   ├── _home.scss
│   │   └── _about.scss
│   └── main.scss
├── css/
│   └── main.css (generated)
└── index.html

The main.scss file imports all the partials (files starting with _):

main.scss

// Base
@import 'base/variables';
@import 'base/reset';
@import 'base/typography';

// Layout
@import 'layout/header';
@import 'layout/footer';
@import 'layout/grid';

// Components
@import 'components/buttons';
@import 'components/cards';
@import 'components/navbar';

// Pages
@import 'pages/home';
@import 'pages/about';
Note: Files starting with an underscore (_) are called "partials." SASS won't compile them into separate CSS files - they're meant to be imported into a main file.

Adding SASS Scripts to package.json

If you installed SASS via npm, add convenient scripts to your package.json:

package.json Scripts

{
  "name": "my-project",
  "scripts": {
    "sass": "sass scss/main.scss css/main.css",
    "sass:watch": "sass --watch scss/:css/",
    "sass:build": "sass --style=compressed --no-source-map scss/main.scss css/main.min.css"
  },
  "devDependencies": {
    "sass": "^1.69.5"
  }
}

Now you can run:

  • npm run sass - Compile once
  • npm run sass:watch - Watch for changes
  • npm run sass:build - Build optimized production CSS
Tip: This approach makes your project portable. Anyone can clone it, run npm install, and then npm run sass:watch to start developing immediately.

Common Installation Issues and Solutions

Issue 1: "sass: command not found"

Solution: If you installed globally, make sure npm's global bin folder is in your PATH. Run npm config get prefix to find it, then add [prefix]/bin to your PATH.

Issue 2: Permission Errors on Global Install

Solution: On macOS/Linux, you might need sudo: sudo npm install -g sass. Better yet, configure npm to use a directory you own without sudo.

Issue 3: Old node-sass Instead of Dart Sass

Solution: Make sure you're installing the sass package, NOT node-sass. The latter is deprecated.

Correct Package

# Correct
npm install sass

# Incorrect (deprecated)
npm install node-sass

Conclusion

You now have SASS installed and understand multiple ways to compile SCSS to CSS! You know how to use watch mode for automatic compilation, how to choose output styles, and how to organize your SASS projects professionally.

In the next lesson, we'll dive into SCSS syntax fundamentals and write more complex stylesheets that showcase SASS's power.

Exercise 1: Install and Verify SASS

Install SASS using the npm method:

  1. Verify you have Node.js: node --version
  2. Install SASS globally: npm install -g sass
  3. Verify installation: sass --version
  4. Take a screenshot of the terminal showing the SASS version

Exercise 2: First Compilation

Create your first SCSS file and compile it:

  1. Create a folder called sass-practice
  2. Inside, create test.scss with some variables and nesting
  3. Compile it: sass test.scss test.css
  4. Open both files and compare the SCSS input to the CSS output
  5. Try modifying the SCSS and recompiling to see the changes

Exercise 3: Set Up Watch Mode

Practice using watch mode:

  1. In your sass-practice folder, start watch mode: sass --watch test.scss:test.css
  2. Edit test.scss, save it, and watch the terminal output
  3. Check that test.css updated automatically
  4. Try making several changes and observe instant compilation
  5. Stop watch mode with Ctrl+C

Bonus: Try compiling with --style=compressed and compare the output size to the expanded version!