For Angular 6 check the Official documentation
Note: For @angular/cli versions older than 6.0.0-beta.6 use ng set in place of ng config.
For existing projects
In an existing angular-cli project that was set up with the default css styles you will need to do a few things:
- Change the default style extension to
scss
Manually change in
.angular-cli.json(Angular 5.x and older) orangular.json(Angular 6+) or run:ng config defaults.styleExt=scss
if you get an error: Value cannot be found. use the command:
ng config schematics.@schematics/angular:component.styleext scss
(*source: Angular CLI SASS options)
Rename your existing
.cssfiles to.scss(i.e. styles.css and app/app.component.css)Point the CLI to find styles.scss
Manually change the file extensions in
apps[0].stylesinangular.json
- Point the components to find your new style files
Change the
styleUrlsin your components to match your new file names
For future projects
As @Serginho mentioned you can set the style extension when running the ng new command
ng new your-project-name --style=scss
If you want to set the default for all projects you create in the future run the following command:
ng config --global defaults.styleExt=scss
Answer from Chic on Stack OverflowFor Angular 6 check the Official documentation
Note: For @angular/cli versions older than 6.0.0-beta.6 use ng set in place of ng config.
For existing projects
In an existing angular-cli project that was set up with the default css styles you will need to do a few things:
- Change the default style extension to
scss
Manually change in
.angular-cli.json(Angular 5.x and older) orangular.json(Angular 6+) or run:ng config defaults.styleExt=scss
if you get an error: Value cannot be found. use the command:
ng config schematics.@schematics/angular:component.styleext scss
(*source: Angular CLI SASS options)
Rename your existing
.cssfiles to.scss(i.e. styles.css and app/app.component.css)Point the CLI to find styles.scss
Manually change the file extensions in
apps[0].stylesinangular.json
- Point the components to find your new style files
Change the
styleUrlsin your components to match your new file names
For future projects
As @Serginho mentioned you can set the style extension when running the ng new command
ng new your-project-name --style=scss
If you want to set the default for all projects you create in the future run the following command:
ng config --global defaults.styleExt=scss
As of ng6 this can be accomplished with the following code added to angular.json at the root level:
Manually change in .angular.json:
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
}
sass - Converting all css files to scss in angular 2 - Stack Overflow
How to add scss in an existing angular project - Stack Overflow
Angular-cli from scss to css - Stack Overflow
Is SCSS still beneficial with the latest Angular Material and modern CSS features?
How do I use a CSS to SCSS converter?
Why would I want to use a CSS to SCSS converter?
What is a CSS to SCSS converter, and how does it work?
» npm install schematics-scss-migrate
I just had to do this to an existing project and pieced together a bunch of information from various places, mostly from this similar stackoverflow question. Here's a set of command line actions (that worked for me on Mac OSX)
# rename all your current files
find . -name "*.css" -exec bash -c 'mv "
{1%.css}".scss' - '{}' \;
# change angular cli config file to include styles.scss instead of styles.css
sed -i -e 's/styles.css/styles.scss/g' .angular-cli.json
# now we need to go thru and fix up all the places where ts files refer to .css files
# there’s probably a better way (and will only work if you don’t have multiple css files in a component)
# this is what I did
find ./src -name "*.ts" -exec sed -i -e 's/\(.*styleUrls.*\)\.css\(.*\)/\1\.scss\2/g' {} +
rm -r *.ts-e
# fix for future generates styles
ng set defaults.styleExt scss
# Add node-sass npm module
npm install node-sass --save-dev
# or if you are using yarn, like me:
yarn add node-sass --dev
Step1 :- Rename all css file from css (style.css) to scss (style.scss)
Step2 :- If you are using old version (till 4) than please check .angular-cli.json file and replace css to scss in below code "defaults": { "styleExt": "scss", "component": {} }
if you are using latest version (6) than please check angular.json
file and replace css to scss
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
},
And restart the project
Note :- For angular (till 4) basicall we used sass-loader to convert scss to css
but for latest version (6) we used node-sass to convert scss to css so if you are getting error like (Module build failed: Error: sass-loader requires node-sass >=4. Please install a compatible version) than please use below command
npm rebuild node-sass
In the latest version of Angular (v13), It's npm library migrating from css to scss and vice versa
ng add schematics-scss-migrate
If you created your project choosing the default style option as CSS, it's possible that neither.
ng config defaults.styleExt=scss nor
ng config schematics.@schematics/angular:component.style scss
work, since the Angular CLI will try to change these options in your angular.json file, and they are not created at all with the default CSS option. If none of these options work as a first step, you can add the entry in your angular.json file yourself, under schematics:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"myProject": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
...
This will of course only change the extension for the newly generated components. If you need to migrate your project, you would have to rename all your styles files changing the extension from CSS to SCSS, and also the reference to those files in the @Component directive of each component.