Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Import sass files in angular 6

Tags:

sass

angular

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

like image 430
Hot Zellah Avatar asked Jun 15 '18 15:06

Hot Zellah


People also ask

How to use Sass with angular CLI?

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. ...

What are the rules for importing a CSS file into Sass?

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.

What is relative import in Sass?

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.

How do I get the angular CLI to generate CSS files?

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.


2 Answers

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

angular.json

"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

styles.sass

Note: Don't include the file extension or an initial ~.

@import 'variables'; // Imports from src/sass @import 'mixins; 
like image 92
Andy Braham Avatar answered Oct 10 '22 10:10

Andy Braham


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

like image 44
Hot Zellah Avatar answered Oct 10 '22 11:10

Hot Zellah