Transforming CSS to SCSS using Angular-cli

After going through the documentation, I learned that in order to utilize scss, it is necessary to execute the following command:

ng set defaults.styleExt scss

However, even after executing this command and creating the file, I continue to encounter the following error message in my console:

styles.bundle.js:33Uncaught Error: Module build failed: Error: ENOENT:
no such file or directory, open
'/Users/Egen/Code/angular/src/styles.css'(…)

Answer №1

For Angular 6, be sure to consult the Official documentation

Note: If you are using versions of @angular/cli prior to 6.0.0-beta.6, make sure to use ng set instead of ng config.

For pre-existing projects

If you have an existing angular-cli project with default css styles, there are some steps you need to take:

  1. Switch the default style extension to scss

You can manually change it in .angular-cli.json (for Angular 5.x and older) or angular.json (for Angular 6+) or run the command:

ng config defaults.styleExt=scss

If you encounter an error stating "Value cannot be found," use this command:

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

(*source: Angular CLI SASS options)

  1. Rename your current .css files to .scss (e.g., styles.css and app/app.component.css)

  2. Ensure that the CLI is directed to locate styles.scss

Manually update the file extensions in apps[0].styles within angular.json

  1. Adjust the components to find your new styling files

Modify the styleUrls in your components to match the updated file names

For upcoming projects

Following @Serginho's suggestion, specify the style extension when initiating the ng new command

ng new your-project-name --style=scss

To establish a default for all future projects, execute the following command:

ng config --global defaults.styleExt=scss

Answer №2

To achieve this in ng6, you can simply insert the provided code into the angular.json file located at the root directory:

If you want to make manual changes in .angular.json, use the following snippet:

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

Answer №3

Access the angular.json document.

1. Modify the following:

"schematics": {}

to

"schematics": {
           "@schematics/angular:component": {
                   "styleext": "scss"       
             }  
  }
  1. Make adjustments in two locations:

"src/styles.css"

to

"src/styles.scss"

then review and retitle all .css documents and revise component.ts files styleUrls from .css to .scss

Answer №4

If you are using the latest version of Angular (v9), make sure to include the following code snippet in your angular.json file:

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

To add this configuration, simply run the command below:

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

Answer №5

To begin, add the following to your project:

npm i --save-dev schematics-scss-migrate

If you are using an Angular CLI project:

execute the command below:

ng g schematics-scss-migrate:scss-migrate

This command will perform the following tasks within your project:

  1. Change the names of all stylesheets in the src folder recursively.
  2. Modify the styleUrls in corresponding component classes to reference the new stylesheet filenames.
  3. Update the component styles schematics value in the angular.json file or create one if it doesn't already exist.
  4. Replace all references to styles.css with styles.scss in the angular.json file.

I personally tested this on my angular 9 project and was successful...it eliminates the need for manual renaming of files. 😎 Simply input the command and BAM!!! your project is now migrated to a scss project.

for more information: https://www.npmjs.com/package/schematics-scss-migrate

Answer №6

Steps for transitioning existing projects:

In the angular.json file:

  1. Within the build section and the test section, make the following changes:

    Replace "styles": ["src/styles.css"] with "styles": ["src/styles.scss"]

  2. Replace "schematics": {} with

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

The command

ng config schematics.@schematics/angular:component.styleext
  scss
can be used, but it doesn't update the configuration in the same location.

In your project, rename your .css files to .scss

For new projects, use the following command to set up everything:

ng n project-name --style=scss

Regarding global settings

Newer versions may not have a global command for this specific configuration change.

Answer №7

When working with Angular 6,

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

It's important to remember that @schematics/angular is the default schematic for the Angular CLI.

Answer №8

Angular CLI Integration with CSS Preprocessors: Version 6.0.3

If you are starting a new project, you have the option to choose the file extension for your style files:

ng new sassy-project --style=sass

You can also change the default style setting for an existing project:

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

For more information on integrating major CSS preprocessors with Angular CLI, refer to the Angular CLI Documentation.

Answer №10

To make this modification in "angular.json", you will need to manually adjust the following code located at the end of the file.

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

Please ensure that all files are renamed from .css to .scss and update all references accordingly.

Answer №11

To change the default style extension to SCSS, you can use the following command:

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

Answer №12

There are multiple approaches to achieve this task, however, I encountered a new schematics value in the recent angular versions that needed to be preserved. The command

ng config schematics.@schematics/angular:component.styleext scss
failed due to this additional value, so I manually updated the new value in angular.json. Here is the revised value that needs to be inserted:


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

The final version of angular.json will look like this:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  ...
  "projects": {
    "yourapp": {
      ...
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        },
        "@schematics/angular:component": {
          "style": "scss"
        }
...

Lastly, you will need to

  • rename all the css files to scss and
  • update all references in your files accordingly.

Following these steps should complete the process successfully.

Answer №13

A forceful adjustment can be implemented. Although it may take some time, this method will be effective for making changes.

Navigate to the application directory src/app

  1. Access the following file: app.component.ts

  2. Modify the code

    styleUrls: ['./app.component.css']
    to
    styleUrls: ['./app.component.scss']

  3. Save your changes and close the file.

Within the same directory src/app

  1. Change the file extension of app.component.css to (app.component.scss)

  2. Apply this adjustment to all other components as well (e.g., home, about, contact, etc...)

The configuration file angular.json is up next, located at the root of the project.

  1. Find and Replace the css extension with (scss).

  2. Save your changes and close the file.

Finally, restart your application using ng serve -o.

If there are any errors during compilation, repeat the steps outlined above.

Be sure to carefully follow the instructions within the app/src directory.

Answer №14

If you're working with ng6, it's essential to utilize the following command as recommended in a related discussion:

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

Answer №15

If you are using the Nrwl extensions and happen to stumble upon this discussion, just know that all commands are handled by Nx before being passed on to AngularCLI (for example:

ng generate component myComponent
).

To enable SCSS in an Nx workspace, use the following command:

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

Answer №16

For Angular 11 and higher versions

To make this change manually, go to angular.json.

Find the line containing "schematics": {},

Replace it with:

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

Answer №17

If you are using Angular version 12, simply change the name of your styles.css file to styles.scss and don't forget to update angular.json by replacing src/styles.css with src/styles.scss

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to retrieve the date value from an input field?

I am curious about how to extract the date from an input field and compare it with a list of objects. <input type="date" [(ngModel)]="date"> I am looking for a way to retrieve the selected date value and then compare it with the dates in my list of ...

How to manipulate dates using Angular 2's date pipe to add or subtract hours

I am encountering an issue with the new Angular 2 Date Pipe. My date value is as follows: let myDate = '2017-01-31T17:53:36' When I use the Date Pipe to format it in the view like this; {{myDate | date: 'dd/MM/yyyy HH:mm' }} It dis ...

Is there a way to incorporate the Bootstrap 4.0.0 Collapse feature into an HTML table?

Hi there! I have a question about adding collapsible elements to an HTML table. Below is my code: <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b7babaa1a6a1a7b4 ...

How can Angular2's templateUrl and styleUrls utilize relative paths?

As I was searching, I came across something called moduleId which is used to define relative paths for template and CSS files in Angular2 components. However, I'm unsure of the exact way to implement moduleId in our Angular2 components. The issue I&a ...

Invalid Angular form inputs are inoperative but contain valid values

Recently, I created an Angular form featuring two number inputs and a select input. The challenge I've encountered is that even though the model values are being correctly populated in these inputs, the two numbers inputs remain marked as invalid unti ...

An introduction to integrating SASS with Laravel 8

My current project in Laravel 8 requires the use of SASS. I have placed my scss code under resources/sass/app.scss and added the following lines to webpack.mix.js: const mix = require('laravel-mix'); mix.js('resources/js/app.js', &apos ...

Designing a Flexbox Square Dependent on Parent DOM's Smallest Dimension Using Bootstrap and React

Is there an efficient method to create a square card or div using Bootstrap flexboxes with minimal custom CSS and/or utilizing built-in Bootstrap properties if feasible? My aim is to achieve a responsive square positioned in the center of this card, based ...

Using Jquery to switch between different CSS styles

I'm currently working with a jQuery code that is functioning properly. $(window).load(function() { $('.menuBtn').click(function(e) { e.preventDefault(); (this.classList.contains('is-active') === true) ? this.c ...

Avoiding DOM replacement when using ngFor asynchronously with AngularFire2 in Angular 2

In my angular2 application with angularfire2, I am dealing with an async list of messages. <message *ngFor="let message of messages | async" [message]="message"></message> Whenever the list gets updated, all elements in the ngFor loop appear ...

Carousel-Owl, glide through two items simultaneously

I am currently in the process of developing a slider using the beta version of Owl-Carousel 2, but I am encountering several issues. My goal is to configure the owlCarousel to function as follows: It should scroll through 2 items at a time while displayin ...

The ngx-quill module fails to properly sanitize HTML strings

Can someone assist me with this issue involving HTML insertion? In my code, I have: htmlBody = '<p style="color: red;">abc</p>' However, when I view the output, I only see <p>abc</p>. I'm wondering if Angular or ng ...

Ways to declare a function within the events onMouseOver and onMouseOut

<div align="right" style="border:1 #FF0 solid; background-color:#999" onMouseOver="javascript: function(){this.style.backgroundColor = '#DDD';}" onMouseOut="javascript: function(){this.style.backgroundColor = '#999';}"> Register & ...

What techniques can I implement using JavaScript or CSS to create a slightly transparent effect on my text?

I am currently developing a website that features changing background images. I am looking to have the text on the site mimic the color of the backgrounds, but not so much that it becomes difficult to read. How can I make the text transparent in this man ...

I am unable to line up every item next to one another

Regarding the topic at hand, I am facing an issue with aligning multiple objects that should be enclosed in i tags side by side. Despite attempting to use li tags and float:left and right in CSS, the position remains unchanged. Can anyone provide guidanc ...

Creating a horizontal scrollbar for multiple rows and columns in Bootstrap: A step-by-step guide

I need a solution where multiple rows and columns can be scrolled horizontally using just one scroll bar. I have already tried implementing the scrollbar for only one row with multiple columns, but it didn't work as expected. Here is the code that I a ...

Firefox compatibility issue: Bootstrap modal button not functioning properly when wrapping other HTML elements

Hey everyone, I've come up with some awesome image hover effects that you can use. I've implemented bootstrap modals so that when you click on 'show code', a pop-up will display with the HTML and CSS codes for easy copy-pasting. However ...

The curious case of CSS nth-of-type quirks

I am using CSS nth-of-type(even) to organize divs. It usually works fine, but when I send an AJAX request and add new HTML div after another one, the selector starts behaving strangely. .row > .cd-timeline-block-sort:nth-of-type(even) > .cd-timeline ...

What are the steps to adjust text size using a slider?

I'm having trouble coding a feature that changes the text size dynamically with a slider, and it's not working as expected. $('input').on('change', function() { var textSize = $(this).val(); $('.userText p').c ...

Utilizing NGRX to inject reducer state into components

In my current setup, I have a tasks reducer that holds the following structure: { error: false,     loading: false,     tasks: [] } Now, this object is being passed down to a simple component like this: <task-list tasks="tasks$ | async"> ...

Managing button state with checkbox click event in Angular 2: Enabling and disabling buttons effortlessly

Here is the code I am working with: <input type="checkbox" name="vehicle" value="Bike" > <button class="pull-left" (click)='openup()' [Disabled]='button'>Delete</button> This is the corresponding TypeScript code: bu ...