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

"Expected 'styles' to be an array of strings." when a stylesheet is referenced in a Component's styleUrls[] and .angular-cli.json's app[0].styles[]

Open michaelsanford opened this issue 9 years ago • 38 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Versions.

λ ng --version && node -v && npm -v                         
    _                      _                 ____ _     ___ 
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | | 
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | | 
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/                                        
@angular/cli: 1.2.6                                         
node: 8.1.2                                                 
os: win32 x64                                               
@angular/animations: 4.3.2                                  
@angular/common: 4.3.2                                      
@angular/compiler: 4.3.2                                    
@angular/core: 4.3.2                                        
@angular/forms: 4.3.2                                       
@angular/http: 4.3.2                                        
@angular/platform-browser: 4.3.2                            
@angular/platform-browser-dynamic: 4.3.2                    
@angular/router: 4.3.2                                      
@angular/cli: 1.2.6                                         
@angular/compiler-cli: 4.3.2                                
@angular/language-service: 4.3.2                            
v8.1.2
5.3.0

Repro steps.

With an SCSS-based project, include a SCSS stylesheet file reference in both a @Component decorator's styleUrls[] array and .angular-cli.json's app[0].styles[] array.

Trivial example:

app.component.ts

@Component({
  selector: 'my-app',
  template: `
    <router-outlet></router-outlet>
    <message-banner *ngIf="showMessage" [messageContainer]="messageContainer"></message-banner>
  `,
  styleUrls: ["./assets/scss/my/styles.scss"],
  providers: [AuthenticationService, AuthenticationGuard, MessagingService]
})

concurrently with

.angular-cli.json

"styles": [
  "./../node_modules/bootstrap/dist/css/bootstrap.min.css",
  "./../node_modules/font-awesome/scss/font-awesome.scss",
  "./assets/scss/my/loadingPage.scss",
  "./assets/scss/my/styles.scss"
],

The log given by the failure.

Uncaught Error: Expected 'styles' to be an array of strings.
    at assertArrayOfStrings (compiler.es5.js:3920)
    at CompileMetadataResolver.webpackJsonp.../../../compiler/@angular/compiler.es5.js.CompileMetadataResolver.getNonNormalizedDirectiveMetadata (compiler.es5.js:15174)
    at CompileMetadataResolver.webpackJsonp.../../../compiler/@angular/compiler.es5.js.CompileMetadataResolver._getEntryComponentMetadata (compiler.es5.js:15891)
    at compiler.es5.js:15473
    at Array.map (<anonymous>)
    at CompileMetadataResolver.webpackJsonp.../../../compiler/@angular/compiler.es5.js.CompileMetadataResolver.getNgModuleMetadata (compiler.es5.js:15473)
    at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._loadModules (compiler.es5.js:26799)
    at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileModuleAndComponents (compiler.es5.js:26772)
    at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler.compileModuleAsync (compiler.es5.js:26701)
    at PlatformRef_.webpackJsonp.../../../core/@angular/core.es5.js.PlatformRef_._bootstrapModuleWithZone (core.es5.js:4536)

Breaking at compiler.es5.js#assertArrayOfStrings() shows the errant entry in values[] is {}, along with the other stringified styles.

When the duplicate reference is removed from all Component decorators, it appears to work as expected.

Desired functionality.

Any of:

  • That the redundant style would fall out of the tree when shaken and would not produce an error, or
  • A more informative error message in the console (though looking at compiler.es5.ts#assertArrayOfStrings the cause may be exceedingly difficult to ascertain), or
  • Better documentation on global styling indicating allowed values, caveats and restrictions. (I'd be happy to submit a PR for that).

Mention any other details that might be useful.

This application structure exists because we are porting an old angular 2 beta-6 application built with gulp to angular 4.3.2 built with ng.

While these errors are almost certainly artifacts of legacy practices, I found it exceedingly difficult to isolate the cause. While ng itself may mitigate a large portion of the instances of this issue, it remains an often-cited error message on SO, each with a different cause.

michaelsanford avatar Aug 02 '17 19:08 michaelsanford

This error could be caused by a improper (ie too new) version of raw-loader.

Setting it to 1.0.0 fixed the issue in my project.

jlowcs avatar Jun 28 '19 06:06 jlowcs

I was facing same issue in ionic project, make sure you import the scss file in global.scss file. for angular you need to import a scss file in app.scss global.scss file @import 'app/components/ui-components/ui-components.page'; angular.json "styles": [ { "input": "src/theme/variables.scss" }, { "input": "src/global.scss" }, { "input": "src/app/app.scss" } ]

sri037 avatar Jul 01 '19 06:07 sri037

Quick and dirty fix:

Example (app/components/dasboard.component): -> Expected 'styles' to be an array of strings

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['../../../styles.scss']
})
export class DashboardComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

Fixed Version

import { Component, OnInit } from '@angular/core';

const styles = '../../../styles.scss';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: [styles]
})
export class DashboardComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

nxpatterns avatar Jul 28 '19 14:07 nxpatterns

@webia1 It really works, but could you tell me why? 'something' is also a string type, it is the same as string value of variable const something = 'something';, So I can’t understand why there is an error in the first case

Quick and dirty fix:

Example (app/components/dasboard.component): -> Expected 'styles' to be an array of strings

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['../../../styles.scss']
})
export class DashboardComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

Fixed Version

import { Component, OnInit } from '@angular/core';

const styles = '../../../styles.scss';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: [styles]
})
export class DashboardComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

kelian9 avatar Aug 02 '19 23:08 kelian9

Is '../../../styles.scss' the global style sheet for the application? If so, that file should not be used as a component style. They exist in completely separate contexts. Global stylesheets are extracted from the application bundle which is why it doesn't work. The fixed version is essentially hiding the stylesheet from the static analyzer and causing the stylesheet to be added to the application twice (or more depending on how many components are using the global stylesheet). Global stylesheets tend to be quite large and this duplication can cause a large increase in the overall size of the application.

clydin avatar Aug 03 '19 00:08 clydin

Has this problem been solved?Angualr8.2.0 has a similar problem

BerQin avatar Aug 12 '19 10:08 BerQin

** Quick Fix**

I was facing the same issue in Angular 8 Project. I used @webia1 's solution but it caused the new error call, Uncaught Error: The template specified for component AppComponent is not a string

I'm using the latest Angular versions as below👇

"@angular-devkit/core": "^8.2.1", "@angular/animations": "8.2.1", "@angular/cdk": "8.1.2", "@angular/common": "8.2.1", "@angular/compiler": "8.2.1", "@angular/core": "8.2.1",

After so many investigations, I assume that this error may be caused by raw-loader or less loder.

, therefore, I downgraded both raw-loader and less loder versions.

ishaniDolawatta avatar Aug 14 '19 18:08 ishaniDolawatta

@ishaniDolawatta Which version of raw-loader are you using?

arekgotfryd avatar Aug 29 '19 13:08 arekgotfryd

This should have been solved in 8.3

Can someone confirm if this Is still reproducible?

alan-agius4 avatar Aug 29 '19 15:08 alan-agius4

@ishaniDolawatta Which version of raw-loader are you using?

@arekgofi I'm using , raw-loader: 1.0.0 with angular 8.2.1

ishaniDolawatta avatar Aug 29 '19 17:08 ishaniDolawatta

This should have been solved in 8.3

Can someone confirm if this Is still reproducible?

It's still reproducible on angular 8.2.4

mriegr avatar Sep 12 '19 08:09 mriegr

** Quick Fix**

I was facing the same issue in Angular 8 Project. I used @webia1 's solution but it caused the new error call, Uncaught Error: The template specified for component AppComponent is not a string

I'm using the latest Angular versions as below

"@angular-devkit/core": "^8.2.1", "@angular/animations": "8.2.1", "@angular/cdk": "8.1.2", "@angular/common": "8.2.1", "@angular/compiler": "8.2.1", "@angular/core": "8.2.1",

After so many investigations, I assume that this error may be caused by raw-loader or less loder.

, therefore, I downgraded both raw-loader and less loder versions.

thank you, after investigation I found that raw-loader 3.*.* was used in "@angular-devkit/build-angular": "~0.803.3", so after I downgraded that to ~0.802.2 everything worked like a charm.

rainmak33r avatar Sep 20 '19 18:09 rainmak33r

In a ~9.0.0-next.5 CLI app, using the following config:

            "styles": [
              "src/styles.css",
              "src/app/app.component.css"
            ],

I see this error on build:

$ ng build

chunk {main} main-es2015.js, main-es2015.js.map (main) 623 bytes [initial] [rendered]
chunk {polyfills} polyfills-es2015.js, polyfills-es2015.js.map (polyfills) 678 bytes [initial] [rendered]
chunk {polyfills-es5} polyfills-es5-es2015.js, polyfills-es5-es2015.js.map (polyfills-es5) 441 kB [initial] [rendered]
chunk {runtime} runtime-es2015.js, runtime-es2015.js.map (runtime) 6.16 kB [entry] [rendered]
chunk {styles} styles-es2015.js, styles-es2015.js.map (styles) 11.9 kB [initial] [rendered]
Date: 2019-10-01T15:54:15.339Z - Hash: 0a7ddf4facb6e510b294 - Time: 5574ms

ERROR in document is not defined

filipesilva avatar Oct 01 '19 15:10 filipesilva

@filipesilva I got the same error today while using 8.2.10 version in a custom webpack configuration for production, which makes use of the AngularCompilerPlugin to enable AOT.

Also, even if I managed to make the project work with JIT, as soon as I use the @ngtools/webpack to handle my typescript files plus the AngularCompilerPlugin, Webpack bundles the project but I do get a black screen while trying to load by using webpack-dev-server.

Any insight of what is happening? Should I open a new issue to provide more info of my project?

Cheers!

KingDarBoja avatar Oct 11 '19 16:10 KingDarBoja

@KingDarBoja what you're describing seems to be a different problem than the one on this issue. The best way to report it would be to open a new issue with a reproduction so that we can look at it.

filipesilva avatar Oct 11 '19 16:10 filipesilva

@KingDarBoja what you're describing seems to be a different problem than the one on this issue. The best way to report it would be to open a new issue with a reproduction so that we can look at it.

Sure, but just to address something related to this issue, I got the same "Expected 'styles' to be an array of strings." error while using [email protected].

The proper fix I found was described at this comment made by myself, on the EDIT N°2 item.

Hope it helps.

KingDarBoja avatar Oct 11 '19 16:10 KingDarBoja

This change seems to break importing of less files in typescriptfiles

import example from './my-styles.less';

Is there any workaround? Is importing styles files completely unsupported and only worked in >13.0 because of coincidents?

oBusk avatar Dec 13 '21 17:12 oBusk

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.