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.

Garfield550
  • 584
  • 1
  • 8
  • 13

4 Answers4

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
  }
}
Akora
  • 756
  • 7
  • 22
Nicolas
  • 1,125
  • 13
  • 20
  • 7
    note that the cli needs to be restarted for this to take effect – phil294 Dec 30 '16 at 15:24
  • 12
    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 `--aot` – Drenai Dec 26 '17 at 13:29
  • @Drenai What does the `--save` flag do, and what does the '~' mean? I did an npm install without `--save` but it doesn't work. – Pim Oct 12 '18 at 19:48
  • 1
    --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 folder – Drenai Oct 12 '18 at 21:13
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';
    
Akora
  • 756
  • 7
  • 22
elpddev
  • 4,314
  • 4
  • 26
  • 47
  • 1
    Worked like a charm, added into styles.scss – Rip3rs Aug 06 '17 at 17:01
  • 10
    You can leave out the second `normalize.css`, just the first one is sufficient – Drenai Dec 26 '17 at 13:30
  • 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 https://stackoverflow.com/questions/6887336 – Simon_Weaver Jun 05 '18 at 20:00
  • 1
    This will be deprecated on new versions of node-sass – distante Dec 12 '18 at 12:41
  • @distante Thanks. Plans [are](https://sass-lang.com/blog/the-module-system-is-launched) 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](https://github.com/sass/node-sass/issues/2362#issuecomment-386846210) to allow importing from node_modules. – Noumenon Jun 09 '20 at 14:47
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"
  ],
LocalPCGuy
  • 6,006
  • 2
  • 32
  • 28
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">
Kiss Robert
  • 201
  • 3
  • 7