Simply cannot use @/ imports inside scss files.
my variables are located at src/styles/_variables.scss
tsconfig.json:
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"baseUrl": ".",
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./src/*"],
"@/styles/*": ["styles/*"],
"@/components/*": ["components/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
next.config.mjs:
/** @type {import('next').NextConfig} */
const nextConfig = {
sassOptions: {
includePaths: [path.join(__dirname, "styles/")],
},
};
export default nextConfig;
What am I doing wrong? I can't for the life of me figure this out
As far as I've seen, this is pretty much everything you need to do to use absolute imports inside an .scss file.
This is simpler than it seems, you just have to define the variables you want in your
_varaibles.scssfile, example:Then import the _varaibles in the .scss file you want, remember to import with the relative path, for example if your
_varaibalesfile is located insrc/styles/you have to import the complete relative path, it would look like this:@import "src/styles/_varaibles.scss";. Then simply use the variables in the file.Example:
You can also use
@useinstead of@import. If you decide to use@use, you just have to call the file without the extension and the underscore, then to use the variables you have to use the wordvaraiblesbefore.Example:
I hope this helps you, the version of next,js that I am using is
14.1.2and that of sass is^1.72.0.If you have any other questions, leave me a comment and I'll be happy to help.
PS: No need to make additional configurations in
next.config.mjsfile.