365

I'm new to Angular and I'm coming from the Ember community. Trying to use the new Angular-CLI based on Ember-CLI.

I need to know the best way to handle SASS in a new Angular project. I tried using the ember-cli-sass repo to see if it would play along since a number of core components of the Angular-CLI are run off of Ember-CLI modules.

It didn't work but then again not sure if I just misconfigured something.

Also, what is the best way to organize styles in a new Angular project? It would be nice to have the sass file in the same folder as the component.

JDillon522
  • 19,046
  • 15
  • 47
  • 81
  • 3
    You could have a look how it's used in https://github.com/angular/material2 for example the button component https://github.com/angular/material2/tree/master/src/components/button – Günter Zöchbauer Mar 25 '16 at 12:51

18 Answers18

584

Angular CLI version 9 (used to create Angular 9 projects) now picks up style from schematics instead of styleext. Use the command like this:
ng config schematics.@schematics/angular:component.style scss
and the resulting angular.json shall look like this

"schematics": {
   "@schematics/angular:component": {
      "style": "scss"
    }
  }

Other possible solutions & explanations:

To create a new project with angular CLI with sass support, try this:

ng new My_New_Project --style=scss 

You can also use --style=sass & if you don't know the difference, read this short & easy article and if you still don't know, just go with scss & keep learning.

If you have an angular 5 project, use this command to update the config for your project.

ng set defaults.styleExt scss

For Latest Versions

For Angular 6 to set new style on existing project with CLI:

ng config schematics.@schematics/angular:component.styleext scss

Or Directly into angular.json:

"schematics": {
      "@schematics/angular:component": {
      "styleext": "scss"
    }
}
Community
  • 1
  • 1
Mertcan Diken
  • 14,483
  • 2
  • 26
  • 33
  • 18
    Then you can change it from angular.cli.json in this file there is "defaults": { "styleExt": "css", "prefixInterfaces": false, "inline": { "style": false, "template": false } You can change the styleExt – Mertcan Diken Nov 16 '16 at 19:41
  • 147
    `ng set defaults.styleExt scss` – Sebas Jan 30 '17 at 10:54
  • 4
    don't forget to change the `style` property to `scss` in `.angular-cli.json` – OST Oct 18 '17 at 21:01
  • 3
    `ng set defaults.styleExt scss --global` – JoshuaDavid Nov 19 '17 at 20:58
  • and how to do the sourcemapping of scss files ? – Praveen Dec 04 '17 at 04:25
  • $sass sass/screen.scss:stylesheets/screen.css --sourcemap – Mertcan Diken Dec 04 '17 at 09:46
  • @link2pk, for sourcemaps you need to add extra parameter `--extract-css=true` to your `package.json` file. I covered this technique in my article and you can find it [here](https://sergeome.com/blog/2017/08/02/angular-cli-useful-tricks/#preprocessors-ootb). – skryvets Dec 24 '17 at 19:44
  • when you use the default setup, using the command `ng new testApp` will create a `styles.css` in the src folder. After changing to scss, rename the `styles.css` file to `styles.cscc` and check the references in your code. `.angular-cli.json` is crucial. – MichaelHuelsen Jan 26 '18 at 21:29
  • 29
    For Angular 6 to set new style on existing project `ng config schematics.@schematics/angular:component.styleext scss` – Nehal Damania May 22 '18 at 11:50
  • I get an error with `ng config` command above. `Invalid JSON character: "s" at 0:0. Error: Invalid JSON character: "s" at 0:0` – chovy May 26 '18 at 20:32
  • Can you check this issue https://github.com/angular/angular-cli/issues/10715 @chovy – Mertcan Diken Jul 06 '18 at 16:32
  • What about for `PostCSS` ? – RoCkDevstack Oct 02 '18 at 01:41
357

Update for Angular 6+

New Projects

When generating a new project with Angular CLI, specify the css pre-processor as

  • Use SCSS syntax

          ng new sassy-project --style=scss
    
  • Use SASS syntax

          ng new sassy-project --style=sass
    

Updating existing project

Set default style on an existing project by running

  • Use SCSS syntax

          ng config schematics.@schematics/angular:component.styleext scss
    
  • Use SASS syntax

          ng config schematics.@schematics/angular:component.styleext sass
    

    The above command will update your workspace file (angular.json) with

      "schematics": {
          "@schematics/angular:component": {
              "styleext": "scss"
          }
      } 
    

where styleext can be either scss or sass as per the choice.

All Іѕ Vаиітy
  • 24,861
  • 16
  • 87
  • 111
  • 3
    The only thing that gave me trouble with was that `angular-cli.json` is actually `.angular-cli.json`. Once I realized this, I edited it and this is working perfectly. Thank you. – OldTimeGuitarGuy Apr 26 '17 at 18:03
  • @OldTimeGuitarGuy Thanks for pointing it out, the file once named `angular-cli.json` was changed as a hidden file (`.angular-cli.json`) recently (rc2). Fixed. – All Іѕ Vаиітy Apr 26 '17 at 18:13
  • If you're renaming an existing `.css` file you may need to close and reopen it in your editor for it to properly recognize the sass syntax. This is true even with latest Visual Studio, – Simon_Weaver Jan 06 '18 at 10:04
  • Can you confirm if we need to install `node-sass` if using the `Angular-CLI`? From what I can see `ng serve` is serving up some form of CSS, but is `node-sass` required to build the resultant `CSS` on `publish`? – askrich Jan 25 '18 at 14:24
  • Just wanted to note that for updating an existing app to sass, as of Angular 6, get/set have been deprecated in favor of the config command. Rather than `ng set defaults.styleExt scss` you can use `ng config schematics.@schematics/angular:component.styleext scss` https://github.com/angular/angular-cli/wiki/stories-css-preprocessors – Stack Underflow Jun 22 '18 at 20:19
  • Also be aware that on an existing project, you need to rename all the stylesheet files in `*.component.ts` and update the `*.css` references in `angular.json` – Richard Aug 15 '18 at 11:19
  • 1
    For angular 6 node-sass is already installed so no need to install it. – Brent Sep 01 '18 at 13:48
  • 1
    I have the same setup but importing scss files directly into components is not working, while importing them into styles.scss working fine. I am using angular cli verison 7+, is there any bug ? – Hemant Sankhla Jan 16 '19 at 10:30
42

Quoted from Officials github repo here -

To use one just install for example

npm install node-sass

and rename .css files in your project to .scss or .sass. They will be compiled automatically. The Angular2App's options argument has sassCompiler, lessCompiler, stylusCompiler and compassCompiler options that are passed directly to their respective CSS preprocessors.

See here

spiegelm
  • 65
  • 7
regnar
  • 521
  • 3
  • 6
  • 6
    may want to add --save-dev, not sure why it's not in the docs that way but lost it when we did an upgrade and then couldn't figure out why our styles were gone – regnar Jun 17 '16 at 17:35
  • 2
    There was an issue where sass files with leading underscores were also being compiled, but it was fixed as of angular-cli 1.0.0-beta.9 – Andy Ford Jul 07 '16 at 11:38
  • The link has changed to: https://github.com/angular/angular-cli/wiki/stories-css-preprocessors – martin Jun 26 '17 at 13:23
28

Tell angular-cli to always use scss by default

To avoid passing --style scss each time you generate a project, you might want to adjust your default configuration of angular-cli globally, with the following command:

ng set --global defaults.styleExt scss


Please note that some versions of angular-cli contain a bug with reading the above global flag (see link). If your version contains this bug, generate your project with:

ng new some_project_name --style=scss
Michel
  • 26,600
  • 6
  • 64
  • 69
  • I am on angularCLI v. 1.2.1 (latest, as of July 2017) and I have the bug you described. i ran this command, and it appears to work perfectly for new components by default, but new projects still create the app.component.css unless i specify `--style=scss` – Jeremy Moritz Jul 20 '17 at 22:04
25

Like Mertcan said, the best way to use scss is to create the project with that option:

ng new My_New_Project --style=scss 

Angular-cli also adds an option to change the default css preprocessor after the project has been created by using the command:

ng set defaults.styleExt scss

For more info you can look here for their documentation:

https://github.com/angular/angular-cli

Jared Wadsworth
  • 839
  • 6
  • 15
  • 3
    `ng set` does not appear to work on settings in the `apps` array. Eg. `ng set apps.prefix blah` throws a "Unexpected token b in JSON at position 0". – mellis481 Feb 16 '17 at 16:42
17

I noticed a very anoying gotcha in moving to scss files!!! One MUST move from a simple: styleUrls: ['app.component.scss'] to styleUrls: ['./app.component.scss'] (add ./) in app.component.ts

Which ng does when you generate a new project, but seemingly not when the default project is created. If you see resolve errors during ng build, check for this issue. It only took me ~ 1 hour.

Richard
  • 4,341
  • 5
  • 35
  • 55
JoelParke
  • 2,676
  • 2
  • 24
  • 38
7

Best could be ng new myApp --style=scss

Then Angular CLI will create any new component with scss for you...

Note that using scss not working in the browser as you probably know.. so we need something to compile it to css, for this reason we can use node-sass, install it like below:

npm install node-sass --save-dev

and you should be good to go!

If you using webpack, read on here:

Command line inside project folder where your existing package.json is: npm install node-sass sass-loader raw-loader --save-dev

In webpack.common.js, search for "rules:" and add this object to the end of the rules array (don't forget to add a comma to the end of the previous object):

{
  test: /\.scss$/,
  exclude: /node_modules/,
  loaders: ['raw-loader', 'sass-loader'] // sass-loader not scss-loader
}

Then in your component:

@Component({
  styleUrls: ['./filename.scss'],
})

If you want global CSS support then on the top level component (likely app.component.ts) remove encapsulation and include the SCSS:

import {ViewEncapsulation} from '@angular/core';

@Component({
  selector: 'app',
  styleUrls: ['./bootstrap.scss'],
  encapsulation: ViewEncapsulation.None,
  template: ``
})
class App {}

from Angular starter here.

Alireza
  • 100,211
  • 27
  • 269
  • 172
7

ng set --global defaults.styleExt=scss is deprecated since ng6. You will get this message:

get/set have been deprecated in favor of the config command

You should use:

ng config schematics.@schematics/angular:component '{ styleext: "scss"}'

If you want to target a specific project (replace {project} with your project's name):

ng config projects.{project}.schematics.@schematics/angular:component '{ styleext: "scss"}'
aloisdg
  • 22,270
  • 6
  • 85
  • 105
  • Related to [No config found or get/set have been deprecated in angular 6](https://stackoverflow.com/q/50425616/1248177) – aloisdg May 29 '18 at 10:03
7

For Angular 9.0 and newer, update the "schematics":{} object in angular.json file like this:

"schematics": {
  "@schematics/angular:component": {
    "style": "scss"
  },

  // Angular 13 may contain this as well; NOT related
  "@schematics/angular:application": {
    ...
  }
},
Mahozad
  • 18,032
  • 13
  • 118
  • 133
  • Thanks, I was wondering why it didn't work and it turns out they renamed it to `style` now – Dino May 10 '20 at 23:57
5

Angular-CLI is the recommended method and is the standard in the Angular 2+ community.

Crete a new project with SCSS

ng new My-New-Project --style=sass

Convert an existing project (CLI less than v6)

ng set defaults.styleExt scss

(must rename all .css files manually with this approach, don't forget to rename in your component files)


Convert an existing project (CLI greater than v6)

  1. rename all CSS files to SCSS and update components that reference them.
  2. add the following to the "schematics" key of angular.json (usually line 11):

"@schematics/angular:component": { "styleext": "sass" }

Edodso2
  • 122
  • 1
  • 6
5

As of 3/10/2019, Anguar dropped the support of sass. No mater what option you passed to --style when running ng new, you always get .scss files generated. It's a shame that sass support was dropped without any explanation.

Just a learner
  • 26,690
  • 50
  • 155
  • 234
  • 1
    For anyone struggling with this after 3/10/2019: Angular reintroduced sass support for angular-cli in [8.0.0-beta.5](https://github.com/angular/angular-cli/releases/tag/v8.0.0-beta.5) and it should be back into the main release branch soon. You can install the pre-release with `npm install -g @angular/cli@8.0.0-beta.5` – mkrl Mar 13 '19 at 07:07
  • Interesting. Thanks for reminding. Just out of curiosity, do you know why they drop the support in the first place? – Just a learner Mar 13 '19 at 07:10
  • Apparently the Angular team was referring to it as "no longer relevant" syntax, at least that's what was stated in [this](https://github.com/angular/angular-cli/pull/13444) PR by the core team member, couldn't find any further information on this. – mkrl Mar 13 '19 at 07:17
4

The following should work in an angular CLI 6 project. I.e if you are getting:

get/set have been deprecated in favor of the config command.

npm install node-sass --save-dev

Then (making sure you change the project name)

ng config projects.YourPorjectName.schematics.@schematics/angular:component.styleext sass

To get your default project name use:

ng config defaultProject

However: If you have migrated your project from <6 up to angular 6 there is a good chance that the config wont be there. In which case you might get:

Invalid JSON character: "s" at 0:0

Therefore a manual editing of angular.json will be required.

You will want it to look something like this (taking note of the styleex property):

...
"projects": {
    "Sassy": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {
        "@schematics/angular:component": {
          "styleext": "scss"
        }
      }
...

Seems like an overly complex schema to me. ¯_(ツ)_/¯

You will now have to go and change all your css/less files to be scss and update any references in components etc, but you should be good to go.

Neil Watson
  • 2,801
  • 3
  • 18
  • 20
3

Set it as Global Default

ng set defaults.styleExt=scss --global

Sajith Mantharath
  • 2,297
  • 2
  • 17
  • 19
3

For Angular 12 update angular.json with:

"schematics": {
   "@schematics/angular:component": {
      "style": "scss"
    }
  }

and:

"build": {
  "options": {
    ...
    "inlineStyleLanguage": "scss"
  }
}

"test": {
  "options": {
    ...
    "inlineStyleLanguage": "scss"
  }
}
carraua
  • 1,398
  • 17
  • 36
2

TL;DR

Quick note: In Angular 9 & onwards, styleext is renamed to style

Angular 9 & 9+
While creating a new project:
ng new my-proj --style=scss

For an existing one:
ng schematics.@schematics/angular:component.style scss

For Angular 2 - Angular 8:
For an existing project:
ng config schematics.@schematics/angular:component.styleext scss

Detailed answer

Angular 9 & 9+ :

Via Angular CLI
ng config schematics.@schematics/angular:component.style scss
Directly in the angular.json:
"schematics": {
   "@schematics/angular:component": {
   "style": "scss"
   }
}

For older versions

Via the Angular CLI:
ng config schematics.@schematics/angular:component.styleext scss
Directly in the angular.json:

note: lookup angular-cli.json for versions older than 6. For version 6 & 6+ the file has been renamed to angular.json

"schematics": {
   "@schematics/angular:component": {
   "styleext": "scss"
   }
 }

Note: Original SO answer can be found here

Junaid
  • 4,682
  • 1
  • 34
  • 40
  • someone has answered this for existing Angular 6 projects [here](https://stackoverflow.com/questions/50619327/using-scss-sass-as-default-style-sheet-in-angular-6-styleext) – padigan Jun 04 '18 at 19:02
1

CSS Preprocessor integration Angular CLI supports all major CSS preprocessors:

To use these preprocessors simply add the file to your component's styleUrls:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'app works!';
}

When generating a new project you can also define which extension you want for style files:

ng new sassy-project --style=sass

Or set the default style on an existing project:

ng config schematics.@schematics/angular:component.styleext scss

note: @schematics/angular is the default schematic for the Angular CLI

Style strings added to the @Component.styles array must be written in CSS because the CLI cannot apply a pre-processor to inline styles.

Based on angular documentation https://github.com/angular/angular-cli/wiki/stories-css-preprocessors

-1

Step1. Install bulma package using npm

npm install --save bulma

Step2. Open angular.json and update following code

...
 "styles": [
 "node_modules/bulma/css/bulma.css",
 "src/styles.scss"
 ],
...

That's it.

To easily expose Bulma’s tools, add stylePreprocessorOptions to angular.json.

...
 "styles": [
 "src/styles.scss",
 "node_modules/bulma/css/bulma.css"
  ],
 "stylePreprocessorOptions": {
 "includePaths": [
    "node_modules",
    "node_modules/bulma/sass/utilities"
 ]
},
...

BULMA + ANGULAR 6

BALA
  • 1,170
  • 1
  • 12
  • 13
  • that's completely off the angular standard, and using scss is just a secondary effect of using bulma – netalex Dec 12 '18 at 12:20
-7

DO NOT USE SASS OR ANGULAR's EMBEDDED CSS SYSTEM!!

I cannot stress this enough. The people at Angular did not understand basic cascading style sheet systems as when you use both these technologies - as with this angular.json setting - the compiler stuffs all your CSS into Javascript modules then spits them out into the head of your HTML pages as embedded CSS that breaks and confuses cascade orders and disables native CSS import cascades.

For that reason, I recommend you REMOVE ALL STYLE REFERENCES FROM "ANGULAR.JSON", then add them back into your "index.html" manually as links like this:

<link href="styles/styles.css" rel="stylesheet" />

You now have a fully functioning, cascading CSS system that's fully cached in your browser over many refreshes without ECMAScripted circus tricks, saving huge amounts of bandwidth, increasing CSS overall management in static files, and full control over your cascade order minus the clunky embedded CSS injected into the head of all your web pages which Angular tries to do.

SASS isn't even necessary when you understand how to manage cascades in basic CSS files. Large sites with complex CSS can be very simple to manage for those that bother to learn cascade orders using a handful of linked CSS files.

Stokely
  • 12,444
  • 2
  • 35
  • 23