Angular build fails with SassError: Undefined variable
Error: Undefined variable.Short answer
Sass modules loaded with @use are scoped to the file that loads them and their variables need a namespace prefix. An Undefined variable error means this file never loaded the module, uses the bare name, or the name starts with an underscore or hyphen. Add the @use rule and prefix the variable.
Fix it fast
1Find out which cause you have
Find where the variable is declared and how the failing file gets it. The line number in the error is the usage, not the declaration.
grep -rn "\$primary-color" src/2Match what you see, then jump to its fix
- 1
The variable works in styles.scss but fails inside a component .scss fileThe failing file never loaded the module with @useFix - 2
@use is present, yet the bare name is still undefinedThe variable is used without its namespaceFix - 3
The variable is declared like $-radius or $_radius and fails only from other filesThe name starts with an underscore or hyphenFix - 4
The same project still has @import lines for style partialsA partial is still pulled in with @importFix
- 1
What does the error look like?
Error: Undefined variable.SassError: Undefined variable.
Cause 1 of 4
The failing file never loaded the module with @use
1Check
Members brought in by @use are visible only in the stylesheet that loads them. Component styles are separate files, so each one needs its own @use. The old @import made everything global, which hid this.
grep -n "@use\|@import" src/app/my-component/my-component.component.scss2Fix
Add the @use rule at the top of the file that uses the variable, before any style rules.
// my-component.component.scss
@use "../../styles/variables";
.title { color: variables.$primary-color; }3Verify
Rebuild; the error for that file is gone.
npx ng buildConfirmed by sass-lang.com
Cause 2 of 4
The variable is used without its namespace
1Check
By default the namespace is the last part of the module's URL. Variables must be written as namespace.$variable, unless the module was loaded with as *.
grep -n "@use" src/app/my-component/my-component.component.scss2Fix
Prefix the variable, pick a short namespace with as, or load your own file without a namespace.
@use "../../styles/variables" as v;
.title { color: v.$primary-color; }
// or, for a file you wrote yourself:
@use "../../styles/variables" as *;
.title { color: $primary-color; }3Verify
Rebuild and check the compiled colour in the browser.
npx ng serveConfirmed by sass-lang.com
Cause 3 of 4
The name starts with an underscore or hyphen
1Check
Names that begin with - or _ are private to the module that defines them. They are not part of its public API.
grep -n "^\$[-_]" src/styles/_variables.scss2Fix
Rename the variable without the prefix, or expose it through a public name or a function in that module.
// _variables.scss
$radius: 3px;3Verify
Use variables.$radius from the other file and rebuild.
npx ng buildConfirmed by sass-lang.com
Cause 4 of 4
A partial is still pulled in with @import
1Check
@import has been deprecated since Dart Sass 1.80.0 and is due to be removed in Dart Sass 3.0.0. It puts members in the global scope, so mixing it with @use gives different results by file order.
grep -rn "@import" src/ | head2Fix
Convert the remaining @import lines to @use, one file at a time, adding the namespace where the variables are used.
// before
@import "variables";
// after
@use "variables";3Verify
No deprecation warning and no undefined variable.
npx ng buildConfirmed by sass-lang.com
How do I stop it coming back?
- Use @use and @forward in new styles; avoid mixing them with @import in one project.
- Keep shared variables in one module and forward it from a single entry file.
- Put @use lines first in every stylesheet, since other rules before them are not allowed.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
Related fixes
Web apps
Your build asks for node-sass and cannot load it
ERROR in Cannot find module 'node-sass'
node-sass is end of life and its repository is archived, so installs on current Node.js often fail or never produce the binary.
3 causes, each with a fixBack end
npm install node-sass fails on a current Node.js version
node-sass fails to install on a recent Node.js version
node-sass is end-of-life and its newest release supports Node.js up to 20, so on newer Node.js the install falls back to a source build that fails.
3 causes, each with a fixBack end
Sass cannot find a stylesheet imported with a leading ~
File to import not found or unreadable: ~bootstrap/scss/bootstrap
The ~ prefix is not part of Sass.
3 causes, each with a fixBack end
webpack reports Module build failed from sass-loader
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
The sass-loader line only says which loader failed.
3 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fix