Importing SCSS modules

Learn how @import, @use, and @forward work in SCSS, with practical guidance for organizing your styles.

🎯 Introduction

This article introduces the SCSS module system. You will learn why to move away from deprecated @import, how to use @use and @forward, and how these tools improve maintainability and reuse. Practical organization guidelines will help keep your project clear and efficient.


🌀 @import (deprecated; included for historical context)

SCSS stylesheets traditionally used @import to load other style files:

@import 'variables';
@import 'mixins';

Although convenient, this approach has several drawbacks:

  • Files can be compiled repeatedly, producing duplicate styles.
  • There is no namespace control, making global variable conflicts more likely.
  • Performance suffers and builds take longer.

To address these problems, Sass introduced its modern module system, @use and @forward, in version 1.23.0. @import has since been deprecated in favor of this system.

If your project still uses @import, migrate to avoid future compatibility issues.

🌟 Using @use

@use is the recommended way to load Sass modules. It loads an SCSS file as a module and applies a namespace by default to avoid variable and mixin conflicts.

Basic syntax

@use 'path/filename' as namespace;

For example:

@use 'variables' as vars;

body {
    color: vars.$main-color;
}

variables.scss defines $main-color, which we access as vars.$main-color.

Omitting the namespace

To use variables from the loaded file directly, omit the namespace with as *:

@use 'variables' as *;

body {
    color: $main-color;
}

This can cause naming conflicts and is particularly discouraged in large projects.

Features of @use

  • Applies a namespace by default to prevent conflicts.
  • Loads each module only once, avoiding duplicate imports.
  • Supports passing configuration values to modules.
  • Keeps members scoped to their module; public members can be accessed through its namespace or re-exported with @forward.

Example: configuring a module

The theme.scss file:

$color: blue !default;

body {
    background-color: $color;
}

Pass configuration values when loading it in the main file:

@use 'theme' with (
    $color: red
);

This lets you override a module's default variables.


🚀 Using @forward

@forward is part of the Sass module system. It re-exports module members, which is especially useful when building style libraries or organizing multiple Sass files.

For example:

Suppose you have several small modules:

  • _variables.scss
  • _mixins.scss
  • _functions.scss

Create a shared entry point, such as index.scss:

index.scss
@forward 'variables';
@forward 'mixins';
@forward 'functions';

Then load that entry point in your main stylesheet:

@use 'index' as *;

All the exported variables and mixins are now available through a single module.

What @forward does

  • Re-exports module members.
  • Can rename exported variables or mixins.
  • Can hide variables that should not be public.

Renaming example

variables.scss
$color-primary: blue;
index.scss
@forward 'variables' as vars-*;
main.scss
@use 'index' as *;

body {
    color: vars-color-primary; // Instead of $color-primary
}

The forwarded variables receive the vars- prefix to prevent naming conflicts.

Hiding members from exports

Use the hide option with @forward:

@forward 'variables' hide $internal-var;

Other modules cannot access $internal-var through this forwarding module.


🏗️ Practical project organization

When organizing SCSS files, consider the following approach:

  1. Keep foundational definitions in _variables.scss, _mixins.scss, and _functions.scss, including colors, fonts, and utility functions.
  2. Use _index.scss as an entry point that re-exports the foundational modules with @forward.
  3. Keep component styles in a separate folder, with one file per component. Load the foundational styles with @use 'index' as *;.

Example directory structure:


scss/
├── base/
│ ├── \_variables.scss
│ ├── \_mixins.scss
│ ├── \_functions.scss
│ └── \_index.scss
├── components/
│ ├── \_button.scss
│ └── \_card.scss
└── main.scss

Contents of _index.scss:

@forward 'variables';
@forward 'mixins';
@forward 'functions';

Contents of _button.scss:

@use '../base/index' as *;

.button {
    background-color: $primary-color;
    @include flex-center;
}

Contents of main.scss:

@use 'base/index' as *;
@use 'components/button';
@use 'components/card';

A small project with only a few styles may not need @forward. As the project grows, using it appropriately can make maintenance much easier.

❓ Questions and answers

Q1: Why should I stop using @import?

@import can compile styles repeatedly, affect performance, and cause variable conflicts. It is deprecated and no longer the approach being developed.

Q2: How do I use variables loaded with @use without a namespace?

Use as *, but be aware that it can cause naming conflicts and is generally discouraged.

Q3: What is the difference between @forward and @use?

@use loads a module for use in the current stylesheet. @forward re-exports its members, typically to create a shared module entry point.


🧪 Exercises

  1. In a new SCSS project, define $main-color: #3498db; in _variables.scss. Load it with @use and apply the color to the background of body.
variables.scss
$main-color: #3498db;
style.scss
@use 'variables' as vars;

body {
    background-color: vars.$main-color;
}
  1. Create _index.scss and use @forward to export _variables.scss and a custom _mixins.scss. Load _index.scss with @use in the main file and include a mixin.
_variables.scss
$primary-color: #e74c3c;
_mixins.scss
@mixin center {
    display: flex;
    justify-content: center;
    align-items: center;
}
index.scss
@forward 'variables';
@forward 'mixins';
style.scss
@use 'index' as *;

.container {
    @include center;
    background-color: $primary-color;
}

🎉 You now understand why @import is deprecated, how to use @use and @forward, and how to organize SCSS modules. These techniques help keep your frontend styles consistent, efficient, and maintainable!