Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Angular build fails with SassError: Undefined variable

ErrorError: 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.

  • Applies to Angular CLI 21, Dart Sass 1.80 or newer, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1The variable works in styles.scss but fails inside a component .scss fileThe failing file never loaded the module with @useFix
    2. 2@use is present, yet the bare name is still undefinedThe variable is used without its namespaceFix
    3. 3The variable is declared like $-radius or $_radius and fails only from other filesThe name starts with an underscore or hyphenFix
    4. 4The same project still has @import lines for style partialsA partial is still pulled in with @importFix

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.scss

2Fix

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 build

Confirmed by sass-lang.com

Did this fix it?

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.scss

2Fix

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 serve

Confirmed by sass-lang.com

Did this fix it?

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.scss

2Fix

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 build

Confirmed by sass-lang.com

Did this fix it?

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/ | head

2Fix

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 build

Confirmed by sass-lang.com

Did this fix it?

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:

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 fix

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.