I created new angular project with sass, and I created folder with name sass which contain a file named _variables.scss,
in app component I tried to import variables like this.
@import 'variables'
when I run ng serve i get the following error:
./src/app/app.component.scss Module build failed:
@import 'variables' ^ File to import not found or unreadable: variables. in C:\Users\Bonge\Documents\Projects\movies_database\movies-client\src\app\app.component.scss (line 1, column 1)
Note I added the following to angular.json:
"stylePreprocessorOptions": { "includePaths": [ "src/", "src/sass/" ]
Directory structure just a angular starter app:
|- src/ |- sass/ |- _variables.scss |- _mixins.scss |- styles.scss
still i get the same error: what am I doing wrong here? any help
Using Sass with the Angular CLI 1 Starting an Angular CLI Project with Sass. Normally, when we run ng new my-app, our app will have .css files. ... 2 Converting a Current App to Sass. ... 3 Using Sass Imports. ... 4 Importing Sass Files Into Angular Components. ... 5 Sass Include Paths. ... 6 Using Bootstrap Sass Files. ... 7 Conclusion. ...
The only rule is that the import must not explicitly include the .css extension, because that’s used to indicate a plain CSS @import. CSS files imported by Sass don’t allow any special Sass features.
Relative imports are always available. As a convention, Sass files that are only meant to be imported, not compiled on their own, begin with _ (as in _code.scss ). These are called partials , and they tell Sass tools not to try to compile those files on their own. You can leave off the _ when importing a partial.
To get the CLI to generate .scss files (or .sass / .less) is an easy matter. You can also set the --style flag with the following: If you’ve already created your Angular CLI app with the default .css files, it will take a bit more work to convert it over.
I realize this is an older question, but keeps coming up in searches so I figure an update is in order. There is a way to define your own import paths for SASS like node_modules
libraries, all you need to do is make a stylePreprocessorOptions
entry in the options
section of the angular.json file. You do not need to include everything using src\sass
"options": { "outputPath": "dist/App", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "src/tsconfig.app.json", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "src/sass/styles.scss" ], "stylePreprocessorOptions": { "includePaths": [ "src/sass" ] }, "scripts": [] },
Then in your styles you can simply import them using
Note: Don't include the file extension or an initial ~
.
@import 'variables'; // Imports from src/sass @import 'mixins;
In the new angular v6, importing sass files is a little different from the previous version.
I just needed to import like this (in a component stylesheet)
@import "~src/sass/variables"; // note: without file extension
Now everything works fine
Thanks all for the help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With