meta.global-variable-exists() belongs to the built-in sass:meta module. It answers: is there a global variable named X? This page covers the no-$ name rule, locals vs globals, $module, vs meta.variable-exists, and five compiled examples.
01
Concept
Is this global defined?
02
Module
@use "sass:meta"
03
Returns
true / false
04
Name
Without $
05
vs locals
variable-exists
06
Practice
5 examples
Concept
What Is meta.global-variable-exists()?
Official docs: returns whether a global variable named $name exists (without the $). Use it for optional theme tokens, safe defaults, and library APIs that only care about root-level configuration.
Without $module, checks for an un-namespaced global in the current file’s context.
With $module, checks a variable inside a @use namespace.
Variables declared inside a selector / mixin / function are local—this helper returns false for them.
💡
Beginner tip
Think of a company directory: global-variable-exists only lists HQ numbers. Desk phones in individual offices (locals) need meta.variable-exists instead.
Foundation
📝 Syntax
styles.scss
@use "sass:meta";
// Recommended
meta.global-variable-exists($name, $module: null)
// Legacy global name
global-variable-exists($name, $module: null)
Parameters
Parameter
Type
Required
Description
$name
String
Yes
Variable name without$. Example: "brand" checks $brand.
$module
String or null
No
If set, must match a @use namespace in the current file.
Return value
Type
Result
Boolean
true if that global exists; otherwise false
Modules
📦 Loading sass:meta
styles.scss
// Recommended — namespaced
@use "sass:meta";
$ok: meta.global-variable-exists("brand");
// Optional — bring members into scope
@use "sass:meta" as *;
$ok: global-variable-exists("brand");
// Legacy global
$ok: global-variable-exists("brand");
Details
🔗 Global vs Local Variables
Official sample: a root $var1 is global; a $var2 inside h1 { … } is local. Only the global is found by this helper.
Where declared
global-variable-exists
variable-exists (same spot)
File root / global scope
true
true
Inside a rule / mixin / function
false
true (in that scope)
Never declared
false
false
Options
📦 Using the $module Argument
After @use "./tokens" as tokens, check that module’s globals with $module: "tokens". The string must match the namespace used in this file.
styles.scss
@use "sass:meta";
@use "sass:math";
// Built-in modules expose members; $e is a known math constant in sass:math
$has-e: meta.global-variable-exists("e", $module: "math");
Support
🛠 Compatibility
This is about Sass compilers, not browsers. The check runs at compile time.
Implementation
global-variable-exists
Dart Sass
Yes — prefer meta.global-variable-exists with modules
LibSass
Yes (legacy global / limited modules)
Ruby Sass
Yes (legacy global / limited modules)
Prefer current Dart Sass so @use + $module work as documented.
$brand exists globally, so .btn uses it. accent was never defined, so .chip takes the slate fallback. For simple overrides, $brand: #2563eb !default; is often enough without an exists check.
Example 4 — Check a Variable in a Module
Use $module for a @use namespace such as sass:math.
Official docs point you to meta.variable-exists when locals matter. Here both checks run in the same scope so the difference is obvious.
Applications
🚀 Real-World Use Cases
Optional theme tokens — apply fallbacks only when a global is missing.
Library configuration — detect whether the consumer set root options.
Module-aware tools — query another file’s globals via $module.
Migration helpers — preferred modern check vs deprecated feature-exists.
Debug / assert — fail early if a required global was never defined.
🧠 How Compilation Works
1
Pass a bare name
Call meta.global-variable-exists("brand") (no $).
Source
2
Sass searches globals
Looks in global scope or the named @use module.
Compile
3
Boolean drives @if
Your stylesheet keeps or skips optional configuration paths.
Branch
4
✓
CSS ships
Browsers never see the check—only finished values.
Watch Out
⚠️ Common Pitfalls
Including $ in the name — pass "brand", not "$brand".
Expecting locals — use meta.variable-exists for scoped variables.
Forgetting $module — namespaced members will not match a bare check.
Wrong namespace string — must match this file’s @use … as name.
Expecting runtime checks — everything resolves when Sass compiles.
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:meta" and the namespaced form
Pass bare names without $
Use this for root configuration / theme tokens
Prefer !default when a simple override API is enough
Reach for variable-exists when locals matter
❌ Don’t
Pass "$name" with a dollar sign
Assume it finds variables inside selectors
Use it for browser @supports questions
Forget $module for @use namespaces
Expect the browser to evaluate the check
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about meta.global-variable-exists()
Ask whether a global variable exists—name without $.
5
Core concepts
📝01
API
global-variable-exists
Call
📦02
Module
sass:meta
@use
✅03
Returns
boolean
Result
🔒04
Scope
globals only
Rule
✓05
Name
no $ prefix
Syntax
❓ Frequently Asked Questions
meta.global-variable-exists($name, $module: null) returns true if a global variable named $name exists, otherwise false. Pass the name without the $.
No. Official docs use the bare name: meta.global-variable-exists("var1") checks $var1.
global-variable-exists only finds globals. variable-exists returns true for locals in the current scope as well. A variable declared inside a rule is local—global-variable-exists is false, variable-exists is true.
When checking a variable from a @use namespace. $module must match that namespace string in the current file.
Yes. global-variable-exists($name, $module: null) is the legacy global name. Prefer meta.global-variable-exists after @use "sass:meta".
No. It runs at compile time. Browsers only see the CSS after your @if branches resolve.
Did you know?
Official Sass docs pair this helper with meta.variable-exists on purpose: the same local $var2 is false for global-variable-exists and true for variable-exists when checked inside that rule.
meta.global-variable-exists() returns whether a named global variable exists. Pass the name without $, use $module for @use namespaces, and switch to meta.variable-exists when you also need locals.