Sass compiler throws 'undefined mixin' error when mixins are kept in seperate folder
Asked Answered
R

7

26

Here is the screenshot of my website structure. Screenshot of project structure

In my mixins file, I have created all the necessary sass mixins.

I have created this mixin for border radius:

 @mixin border-radius($radius) {
  -webkit-border-radius: $radius;
     -moz-border-radius: $radius;
      -ms-border-radius: $radius;
       -o-border-radius: $radius;
          border-radius: $radius;
}

Now when I try to use this for my button class in _button.scss in modules folder , I get undefined variable error. Whereas when I use the same mixin in the _button.scss file itself, I don't get any error.

in my _button.scss file i have included the mixin as under

button{
    background-color: $theme-color;
    border: 0px solid;
    padding: 0.7rem 3rem;
    @include border-radius(2rem);

}

What is the issue exactly. I want to keep all my mixins in a seperate file to keep the structure neat.

Raiment answered 12/4, 2015 at 15:34 Comment(3)
Sass doesn't throw this error for no reason, and it's not because the mixin is "in a separate folder".Deportee
possible duplicate of Why do I get "Undefined mixin 'border-radius'" in Compass?Deportee
Please note: Always declare mixins before using them to avoid the ' undefined mixins ' error. So make sure to use include after the mixin is declared not before that.Julijulia
M
12

you have to include the mixin with @include or @import

https://sass-lang.com/documentation/at-rules/mixin

Marlanamarlane answered 12/4, 2015 at 15:53 Comment(4)
when I am including the mixin in the same file it works well .. but when the mixin is in a seperate _mixin.scss file in seperate modules folder, it doesn't work even after using the "@include" statement. Why so?Raiment
$project-path: absolute-path(".."); see if setting the project path so the compass works properlyMarlanamarlane
the link says page not foundVookles
also, worth noticing, you have to declare the mixin before calling it (that worked for me, coming from Google, hehe)Eastward
P
13

Ad as * end of line

Example:

@use  './mix' as *;
Pily answered 21/8, 2022 at 22:2 Comment(4)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Playoff
This was it for meNalepka
Could you please add further clarification to your answer?Halfsole
This is the only correct answerGraceless
M
12

you have to include the mixin with @include or @import

https://sass-lang.com/documentation/at-rules/mixin

Marlanamarlane answered 12/4, 2015 at 15:53 Comment(4)
when I am including the mixin in the same file it works well .. but when the mixin is in a seperate _mixin.scss file in seperate modules folder, it doesn't work even after using the "@include" statement. Why so?Raiment
$project-path: absolute-path(".."); see if setting the project path so the compass works properlyMarlanamarlane
the link says page not foundVookles
also, worth noticing, you have to declare the mixin before calling it (that worked for me, coming from Google, hehe)Eastward
E
8

Only answering because I got the same error for a different reason.

The solution for me was to reorder my imports. As it turns out I was referencing a mixin that wasn't imported yet. Check to be sure you aren't using a mixin prior to it's being imported.

Electrotherapeutics answered 12/8, 2022 at 19:40 Comment(1)
the same for me, thanksLunneta
I
2

as Waseem Wisa said you add @use 'mix' as *; or you can edit your file like this: '''

@use 'mix';
button{
    background-color: $theme-color;
    border: 0px solid;
    padding: 0.7rem 3rem;
    @include mix.border-radius(2rem);

}

'''

or add index.scss file to your scss folder then forward all module files in it like : _index.scss (this is file name)

`forward 'mix';`

after that add this file to your main.scss

`@use 'index' as *;`
Impale answered 4/1, 2023 at 17:4 Comment(0)
R
1

To elaborate on Brian's answer make sure you import the _mixins.scss file in the main sass file.

On the top simply do this.

// style.scss
@import "mixins";

This will fix the undefined error.

Ramulose answered 17/1, 2023 at 0:16 Comment(0)
V
1

First import the mixin sass file in your style.scss file. Be sure to import the file first before importing the globals and variables file. For example like this, @import "_variables"; @import "_mixins"; @import "_globals"; @import "_header";

Valentinvalentina answered 3/2, 2023 at 9:47 Comment(0)
S
0

This error appeared to me because I positioned the mixins at the end of scss file, moving them to the top, before the include, solved the problem.

Snorkel answered 22/5 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.