Sub topic 2
Installing Dart Sass and compiling files
This chapter assumes you know basic CSS, can run commands in a terminal, and have Node.js with npm installed. You will install the Dart Sass command line, compile one file, and then run watch mode.
Why compilation is needed
Browsers cannot read SCSS. A compiler must turn your .scss files into plain .css files, and the browser loads only the CSS. Dart Sass is the reference implementation of Sass and the only one that receives new features. Older tools such as LibSass and the node-sass package are deprecated, so avoid them in new projects.
Install
The npm package named sass is Dart Sass compiled to JavaScript. Install it globally and check the version:
npm install -g sass
sass --versionOther options are Homebrew (brew install sass/sass/sass) and the standalone binary from the Dart Sass GitHub releases page. A project-local install with npm install --save-dev sass is often better for teams, because everyone then builds with the same version. You run a local install through npx sass or an npm script.
Compile one file
Pass an input path and an output path:
sass src/styles.scss dist/styles.cssSass creates the dist folder if it does not exist. It also writes a source map, dist/styles.css.map, so browser developer tools can point to the original SCSS lines. Two useful flags change this output:
--style=compressedremoves whitespace for production.--no-source-mapskips the map file.
Watch a folder
Giving sass a input:output pair of folders compiles every Sass file in the input folder. Add --watch to keep the process running:
sass --watch src:distSass compiles everything once, then recompiles whenever a file changes. A syntax error is printed in the terminal and the watcher keeps running, so fix the file and save again. Stop the watcher with Ctrl+C.
Partials are not compiled alone
A file whose name starts with an underscore, such as _tokens.scss, is a partial. Folder compilation skips partials, because they are meant to be loaded by other files. Later chapters cover this with @use. If a new file produces no CSS, check whether its name starts with _.
Common errors
| Symptom | Likely cause |
|---|---|
| `sass: command not found` | The global npm bin folder is not on your `PATH`, or you installed locally and need `npx sass`. |
| `Can't find stylesheet to import` | A wrong path in a `@use` rule, or a missing file. |
| No CSS appears for a file | The file is a partial, or it is outside the watched input folder. |
| Browser shows old styles | The HTML links the wrong CSS file, or the browser cache is stale. |
Exercise
You run sass --watch src:dist. You add src/_buttons.scss and save it, but no dist/buttons.css appears. Why, and how would you get CSS from its contents?
Show answer
Files starting with an underscore are partials, and folder compilation skips them. Load the partial from a non-partial file such as src/styles.scss with @use 'buttons';. Its CSS is then included in dist/styles.css. Alternatively, rename the file to buttons.scss if it should compile on its own.