Getting this error when I am using dynamic styleUrls.
var dynamic = environment.booleanCondition ? '-new' : '';
@Component({
selector: 'app-some-thing',
templateUrl: './some-thing.component.html',
styleUrls: [require(`./some-thing${dynamic}.component.scss`).default]
})
I am facing this issue with ng serve
Error: Expected "styleUrls" to be an array of String
After recompiles, error vanishes and application works fine but it throws different error on terminal.
error TS2591: Cannot find name 'require'. Do you need to install type definitions for node? Try
npm i @types/nodeand then addnodeto the types field in your tsconfig.
But even after installing @types/node and adding node, it still don't resolves my previous issue of "styleUrls" to be an array of String.
I have seen this issue posted many times before but none of fixes seems to work for me.
Updated answer:
The OP updated the question that the variable from
environment.tsfile determines which scss file to use. In that case, there are few ways to pick up the file to use:Option 1:
Copy targeted file to a main file via package.json command (independent of
environment.ts)Make the main scss file empty, so that desired css can be copied into it. In package.json, create script like following:
Execute these script before running your build command pipeline per env:
Option 2:
Use
angular.jsonfileReplacements feature as outlined in this answerangular.json:
Original answer:
Instead of doing such non-standard Angular thing, why don't you take a more conventional approach, where you control all of css from one file. If I had to the option, I would do it this way.
let useNewCss = booleanCondition;something.component.scss:
ngClassto wrap the whole html code within desired css classsomething.component.html: