angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

stylePreprocessorOptions loads node_modules instead of scss

Open MickL opened this issue 8 years ago • 20 comments

Using @import 'file' imports from node_modules when the file is named the same way any node module is named.

Angular.json

"stylePreprocessorOptions": {
              "includePaths": [
                "src/styles"
              ]
            },

Existing files

src 
   - styles
          - _globals.scss
          - _rxjs.scss

my-component.scss

@import 'globals';

-> Imports node-module "globals" index.js

my-component.scss

@import 'rxjs';

-> Imports node-module "rxjs" index.js

Version @angular/cli: 6.0.0-rc.7

MickL avatar Apr 30 '18 15:04 MickL

This also happens if file name is 'colors'

iowaz avatar May 04 '18 17:05 iowaz

I can confirm this issue.

AlexBachmann avatar May 17 '18 12:05 AlexBachmann

I can as well. Experienced this bug with an existing project after using the new ng update cli command, as well as with a fresh app generated with ng new (CLI version 6.0.3)

dev-nicolaos avatar May 23 '18 23:05 dev-nicolaos

same with 'base'

tombrudzinski avatar May 29 '18 18:05 tombrudzinski

I spent all day on this...

"stylePreprocessorOptions": {
              "includePaths": [
                "node_modules/foundation-sites/scss"
              ]
            },

... and angular-cli is trying to import some javascript instead of scss.

Please help!

Angular CLI: 6.0.8 Node: 8.9.1 OS: win32 x64

ZbigiMan avatar Jun 07 '18 21:06 ZbigiMan

Any update from the team on this? @hansl @Brocco @filipesilva

dev-nicolaos avatar Jun 21 '18 17:06 dev-nicolaos

@ZbigiMan

I got Angular 6 with Foundation 6 working with a variation of your question/answer...

Include your stylePreprocessorOptions config, then copy

node_modules/foundation-sites/scss/settings/_settings.scss

into your src directory, then edit this line:

@import 'util/util';

to be

@import 'node_modules/foundation-sites/scss/util/util';

In your style.scss you can now do:

@import 'settings'; @import 'foundation'; @include foundation-everything;

I still get a warning I have not worked on yet, but everything compiles.

ethanj avatar Jun 28 '18 01:06 ethanj

Hey @ethanj. The problem of this issue is not exactly the problem @ZbigiMan describes. In fact quite the opposite. The problem is NOT that you cannot add include paths to the stylePreprocessorOptions of the CLI. You can, and it works, as you described.

The problem is, that the loader FIRST tries to load from the node_modules directory and THEN tries to load from the included paths.

That means, if you have a module called "colors" in node_modules, or a module called "base" you cannot load SCSS files with the same name out of your include paths. These SCSS statements will trigger an error, because the compiler doesn't look in your include paths FIRST but instead tries to load the module in node_modules.

@import "base"
@import "colors"

We did not have this behavior in the old version of the CLI and this is why it's breaking existing code.

AlexBachmann avatar Jun 28 '18 08:06 AlexBachmann

Also it tries to import js files, which seems not correct to me, too.

MickL avatar Jun 28 '18 08:06 MickL

same issue...

Invalid CSS after "v": expected 1 selector or at-rule, was "var colors = requir" in /Users/jeromeaksogut/Documents/pva-stack/app/node_modules/colors/lib/index.js (line 1, column 1) ERROR in ./src/app/shared/component/modification-history-list/modification-history-list.component.scss Module build failed: var colors = require('./colors');

aksjer avatar Jun 28 '18 08:06 aksjer

I also noticed that it's problem with sass-loader 7. Downgrading to sass-loader 6 solves the issue, so It's not problem with @angular/cli itself but with dependency - sass-loader.

ZbigiMan avatar Jun 28 '18 08:06 ZbigiMan

Guys if you have the same issue upvote the issue itself. Contributers dont see your +1 comments and dont see your upvotes on the „colors“ thingy.

MickL avatar Jun 28 '18 09:06 MickL

Here is link to webpack sass-loader issue https://github.com/webpack-contrib/sass-loader/issues/556

ZbigiMan avatar Jun 28 '18 13:06 ZbigiMan

If anyone finds this in the short future, until the commits above are merged. I am on:

Angular CLI: 6.2.3
Angular: 6.1.9

Downgrading sass-loader in package.json fixed this issue for me.

  "dependencies": {
        ...
        "sass-loader": "~6.0.7",
        ...

krismeister avatar Sep 28 '18 04:09 krismeister

I'm still having this issue in v7. Wasn't it fixed?

julkue avatar Nov 14 '18 15:11 julkue

Yea, it looks like it's still there in [email protected]

cichacz avatar Dec 14 '18 15:12 cichacz

Same here when upgrading from v6 to Angular v7.

Downgrading sass-loader to 6.0.7 worked for me.

shprink avatar Jan 03 '19 15:01 shprink

My workaround was to prefix my style files with my project name abbreviation to avoid naming collusion with libraries inside node_modules. So

src 
   - styles
          - _globals.scss
          - _rxjs.scss

will become

src 
   - styles
          - _asdf-globals.scss
          - _asdf-rxjs.scss

Then the import is:

@import 'asdf-globals';
@import 'asdf-rxjs';

nyxz avatar Jan 03 '19 15:01 nyxz

Slight digression: For persons trying to use this in multi-app projects, you need to use the full path to the app (project).

e.g.

"stylePreprocessorOptions": {
    "includePaths": [
        "projects/my_app/src/styles"
    ]
}

Took me a few minutes to realise this...

daverickdunn avatar Jan 03 '19 16:01 daverickdunn

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.