Angular 2: How to correctly automatically import normalize.css
Asked Answered
D

4

32

I am a new Angular 2 user, and I have some problems with it.

Traditionally, we could use <link rel="stylesheet" href="node_modules/normalize.css/normalize.css" /> to import a css file, but I want to make Angular 2 to automatically import it using import.

I tried to use the same way when I used Material 2:

// angular-cli-build.js

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'normalize-path/index.js',
    ]
  });
};

// system-config.ts 

const map: any = {
  'normalize': 'vendor/normalize-path',
};

/** User packages configuration. */
const packages: any = {
  'normalize': {main: 'index.js'},
};

// app.component.ts

import { normalize } from 'normalize-path';

The editor will complain:

Cannot find module 'normalize-path'.

And the code won't compile. But I really have no idea what was wrong.

Dahlberg answered 16/7, 2016 at 9:39 Comment(2)
Doesn't make sense: normalize CSS must really be included as link tag, no need to overcomplicate.Alabaster
Sorry, but I just curious about how to use normalize.css with Angular....Dahlberg
H
62

Update for Angular 8

  1. Install the normalize.css library:

    npm install --save normalize.css
    
  2. Import it in your styles.css

    @import '~normalize.css';
    

With the current (1.0.0-beta.15) version of angular-cli, the solution is quite easy:

  1. npm i normalize.css
  2. add "../node_modules/normalize.css/normalize.css" in apps[0].styles in the config file angular-cli.json

Note: If using Angular 7, the config file is now angular.json, and the path to normalise.css in apps[0].styles should be "../node_modules/normalize.css/normalize.css".

Example:

{
  "project": {
    "version": "1.0.0-beta.15",
    "name": "normalize.css-in-angular2"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": "assets",
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "../node_modules/normalize.css/normalize.css",
        "styles.css"
      ],
      "scripts": [],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false
  }
}
Huysmans answered 23/9, 2016 at 20:51 Comment(4)
note that the cli needs to be restarted for this to take effectPurpurin
There is a better method of loading now. In the styles.css (or styles.scss if you use sass) add @import '~normalize.css'; after doing the npm install --save normalize.css. The ~ denotes the import as a node module. The package.json of the 'normalize' module points to the correct .css file. I have tested this with Angular v5, latest CLI, with ng serve --prod and --aotExecutory
@Executory What does the --save flag do, and what does the '~' mean? I did an npm install without --save but it doesn't work.Gullett
--save updates the package.json file to include the package. The ~ tells the scss compiler to look for the imported normalize.css in the node_modules folderExecutory
D
41

Based on this answer, all needed to do was:

  1. Install the normalize.css library:

    npm install --save normalize.css
    
  2. Import it in your styles.css

    @import '~normalize.css';
    
Demonstrator answered 3/8, 2017 at 12:43 Comment(5)
Worked like a charm, added into styles.scssAntaeus
You can leave out the second normalize.css, just the first one is sufficientExecutory
If this appears to you as if it isn't working make sure you understand the scope of normalize.css and what it does and doesn't do. eg. I expected ul tag to be reset to 0 margin and while reset.css does this normalize.css does not. If you're new to normalize.css check this out stackoverflow.com/questions/6887336Bengaline
This will be deprecated on new versions of node-sassIzabel
@Izabel Thanks. Plans are to deprecate @import before October 2021 and remove it a year later. Annoyingly, this syntax doesn't seem to work with @use. The node-sass people are planning to provide an importer to allow importing from node_modules.Cleliaclellan
C
5

The accepted response doesn't seem to be working on app. I needed to remove the ../ in the path name.

The angular.json styles bit should be something like this:

"styles": [
    "node_modules/normalize.css/normalize.css",
    "styles.css"
  ],
Cultured answered 29/7, 2018 at 1:5 Comment(0)
T
0

// angular-cli-build.js

module.exports = function(defaults) {
  return new Angular2App(defaults, {
    vendorNpmFiles: [
      'systemjs/dist/system-polyfills.js',
      'systemjs/dist/system.src.js',
      'zone.js/dist/**/*.+(js|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(ts|js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',
      '@angular2-material/**/*.+(js|js.map)',
      'normalize.css/normalize.css'
    ]
  });
};

and simple add the css link to index.html

// index.html
<link href="vendor/normalize.css/normalize.css" rel="stylesheet">
Tobitobiah answered 17/7, 2016 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.