Sub topic 2
Installing Dart Sass and compiling files
This chapter assumes you know core CSS (selectors, properties, media queries), can run commands in a terminal, and can link a stylesheet from HTML. You do not need to know any Sass syntax yet. By the end you will have a working compile setup that the rest of the tutorial builds on.
Which Sass implementation to install
Dart Sass is the primary implementation of Sass. It gets new features first, and it is the only one that supports the module system (@use and @forward) covered later. Two older implementations still appear in old tutorials and projects:
- LibSass and the
node-sasspackage built on it are deprecated. They lack modern features. - Ruby Sass reached end of life years ago.
If a guide tells you to npm install node-sass, install sass instead. The sass package on npm is Dart Sass compiled to JavaScript, and it exposes the same sass command.
Installation options
| Method | Command | Good for |
|---|---|---|
| npm, global | `npm install -g sass` | Quick experiments on your own machine |
| npm, per project | `npm install --save-dev sass` | Projects shared with a team, since the version is pinned in `package.json` |
| Homebrew (macOS, Linux) | `brew install sass/sass/sass` | Developers without Node.js |
| Chocolatey (Windows) | `choco install sass` | Developers without Node.js |
| Standalone release | Download from the Dart Sass GitHub releases and add the folder to your `PATH` | Machines where no package manager is available |
For anything you commit to version control, prefer the per-project install. Two developers with different global Sass versions can produce different CSS from the same source.
Check the install:
sass --versionThis prints the Dart Sass version number. If your shell reports command not found, either the global install directory is not on your PATH, or you installed per project and need to run the tool through npx sass or an npm script (shown below).
Compiling one file
Create a small project:
mkdir sass-demo
cd sass-demo
mkdir src distSave this as src/styles.scss:
$primary: #3366cc;
.card {
padding: 1rem;
border: 1px solid $primary;
&:hover {
background: rgba($primary, 0.1);
}
}Compile it by giving the command an input path and an output path:
sass src/styles.scss dist/styles.cssSass writes dist/styles.css:
.card {
padding: 1rem;
border: 1px solid #3366cc;
}
.card:hover {
background: rgba(51, 102, 204, 0.1);
}
/*# sourceMappingURL=styles.css.map */Three things happened. The variable was replaced by its value. The nested &:hover was flattened into a normal selector. Sass also wrote dist/styles.css.map and added the comment on the last line, which we cover in the source maps section.
If you omit the output path, Sass prints the CSS to the terminal. That is useful for quick checks:
sass src/styles.scssThe browser never sees the .scss file. Your HTML links the compiled output:
<link rel="stylesheet" href="dist/styles.css">Compiling a whole folder
Compiling files one at a time does not scale. Use the input:output form with two directories:
sass src:distSass compiles every .scss file in src and writes a matching .css file in dist, keeping subfolder structure. It creates dist if it does not exist.
One rule matters here: files whose names start with an underscore, such as _tokens.scss, are partials. Sass does not compile partials into their own CSS files. They exist to be loaded by other files, which a later chapter covers. If you create src/_tokens.scss and no dist/_tokens.css appears, that is correct behavior.
Watch mode
Re-running the command after every edit is tedious. Watch mode compiles once, then recompiles whenever a source file changes:
sass --watch src:distLeave this running in a terminal while you edit. Some points to know:
- A change to a partial recompiles every file that loads it.
- A syntax error prints a message and Sass keeps watching. Fix the file and it recompiles. Add
--stop-on-errorif you want it to exit instead. - Press
Ctrl+Cto stop. - On network drives, containers, or some virtual machines, file-change events can be unreliable. Add
--pollto make Sass check files on a timer.
If you only want to rebuild files that changed since the last run, use --update instead of --watch. Sass compares timestamps and skips files that are already current.
Useful options
| Option | Effect |
|---|---|
| `--style=compressed` | Minified output with no extra whitespace. The default is `expanded`. |
| `--no-source-map` | Skips the `.map` file and the trailing comment. |
| `--embed-source-map` | Puts the map inside the CSS file instead of a separate file. |
| `--load-path=node_modules` | Lets `@use` find files in that folder, such as a framework's SCSS from npm. |
| `--no-charset` | Stops Sass from adding a `@charset` rule or byte order mark when the output has non-ASCII characters. |
| `--quiet` | Hides warnings, including deprecation warnings. |
Options combine freely:
sass --style=compressed --no-source-map src/styles.scss dist/styles.min.cssFor the example above this writes minified CSS on one line, roughly .card{padding:1rem;border:1px solid #36c}.card:hover{background:rgba(51,102,204,.1)}. Sass also shortens colors in compressed mode, so #3366cc becomes #36c.
Source maps
A source map is a file that links lines in the compiled CSS back to lines in your SCSS. Browser developer tools read it, so the Styles panel shows styles.scss:6 instead of styles.css:3. Keep maps on during development. For production you can either skip them with --no-source-map or deploy them only where you want your source readable.
Running Sass from npm scripts
For a project install, define the commands once in package.json so everyone runs the same thing:
{
"name": "sass-demo",
"private": true,
"scripts": {
"build:css": "sass src:dist --style=compressed --no-source-map",
"watch:css": "sass --watch src:dist"
},
"devDependencies": {
"sass": "^1.0.0"
}
}The version range above is a placeholder. Running npm install --save-dev sass writes the real current version for you, so prefer that command over editing the file by hand. Then:
npm run watch:css
npm run build:cssnpm adds node_modules/.bin to the PATH for scripts, so the local sass is found without a global install. Outside scripts, run npx sass --version.
Common errors
sass: command not found. The install location is not onPATH, or the install was local. Trynpx sass --version, or reopen the terminal after a global install.Error: Can't find stylesheet to import.The path in a@useor@importis wrong, or the folder needs--load-path. Paths are resolved relative to the file that contains them.- Output file has no changes. In
--updatemode, check that you saved the file. In watch mode on a network drive, add--poll. - A partial produces no CSS. Expected. Only files without a leading underscore become output files.
- Garbled non-ASCII characters in the browser. Serve the CSS as UTF-8. Sass adds a charset marker by default, so check that you did not use
--no-charsetwithout a matching HTTP header. - Deprecation warnings for
@importor division with/. These are warnings, not failures. Later chapters show the replacements,@useandmath.div.
Checking your setup
A quick test that everything works end to end:
- Run
sass --watch src:distin the project folder. - Change
$primaryinsrc/styles.scssto#cc3333. - Save the file and look at
dist/styles.css. The border color should now read#cc3333.
If that works, your toolchain is ready for the rest of the tutorial.
Exercise
You have a folder scss containing main.scss and _vars.scss. Write one command that compiles the folder into a folder named css, minified, without source maps. Then say which output files you expect.
Show answer
sass scss:css --style=compressed --no-source-map. You expect only css/main.css. The file _vars.scss is a partial, so Sass does not compile it into its own CSS file. Its content appears in the output only if main.scss loads it.
Exercise
You run sass --watch src:dist, then save a file with a typo. The terminal shows an error. What does Sass do next, and how can you change that behavior?
Show answer
Sass reports the error and keeps watching, so the next valid save recompiles the file. To make Sass exit on the first error, add the --stop-on-error option.