311

I'm using Angular 2+ and Angular CLI.

How do I add font-awesome to my project?

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Nik
  • 7,086
  • 6
  • 36
  • 52
  • I have the same issue. It's like the files are ignored by the CLI and not copied when in dev mode. That said, the file are there in the /dist dir, when a build is done. – Thibs Aug 08 '16 at 14:12
  • I do not understand, why do we need to add font-awesome via npm, couldn't we just link to font-awesome cdn? What am I missing? – Rosdi Kasim Jan 13 '17 at 05:37
  • 4
    @RosdiKasim you can link to the cdn form your index file. There are cases when you wouldn't want to though. Corporate projects may not allow external sources; CDN could go down; CLI update may need to update the index.html file so you'll have to make sure it doesn't overwrite your current links; font-awesome could be a dependency for another npm lib; you want to lock font-awesome to a certain version; your build process could depend on it...(and so on, you get the idea) In the end, up to how you want to bring it in. – Nik Jan 13 '17 at 17:05
  • Ok thanks... looks like I am not missing much... I just want to make sure I understand the pros and cons... cheers. – Rosdi Kasim Jan 14 '17 at 10:09
  • See also the official documentation for adding JS or CSS : https://angular.io/guide/using-libraries#adding-a-library-to-the-runtime-global-scope – Guillaume Husta Apr 05 '19 at 10:18

29 Answers29

534

After Angular 2.0 final release, the structure of the Angular2 CLI project has been changed — you don't need any vendor files, no system.js — only webpack. So you do:

  1. npm install font-awesome --save

  2. In the angular-cli.json file locate the styles[] array and add font-awesome references directory here, like below:

    "apps": [
        {
          "root": "src",
          "outDir": "dist",
          ....
          "styles": [
              "styles.css",
              "../node_modules/bootstrap/dist/css/bootstrap.css",
              "../node_modules/font-awesome/css/font-awesome.css" // -here webpack will automatically build a link css element out of this!?
          ],
          ...
      }
      ]
    ],
    

    In more recent versions of Angular, use the angular.json file instead, without the ../. For example, use "node_modules/font-awesome/css/font-awesome.css".

  3. Place some font-awesome icons in any html file you want:

    <i class="fa fa-american-sign-language-interpreting fa-5x" aria-hidden="true"> </i>
    
  4. Stop the application Ctrl + c then re-run the app using ng serve because the watchers are only for the src folder and angular-cli.json is not observed for changes.

  5. Enjoy your awesome icons!
Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
AIon
  • 12,521
  • 10
  • 47
  • 73
  • Great, thanks! You might want to update your HTML code snippet in point 3. Missing some – Lindsay Ward Sep 21 '16 at 13:13
  • yes Lindsay Ward, you are right. Problem is, for some reason stack overflow editor is hiding the <> tags when an i element is involved- even if they are in the special code block section. I delete that knowingly. I don't know how to do it corectly. None of the skipping character method that i know of works. Sorry about that :) – AIon Sep 21 '16 at 17:17
  • This is the correct way, thanks for sharing. Do you know where's this documented? – bjorkblom Sep 21 '16 at 19:05
  • 3
    Can you explain what `addons` does? I see [it's documented as](https://github.com/angular/angular-cli/blob/master/packages/angular-cli/lib/config/schema.json) *"Configuration reserved for installed third party addons."*, but I cannot find any handling [in the Angular-CLI code](https://github.com/angular/angular-cli/search?type=Code&q=addons). – Arjan Nov 02 '16 at 14:15
  • 1
    Unfortunately @Arjan & @bjorkblom - i couldn't find any docs regarding this `addons` filed... This has been in my attention for a while now.. I will update my answer once i find something. – AIon Nov 02 '16 at 15:46
  • 1
    Minor issue, bootstrap.css is included twice in styles – rjdkolb Nov 18 '16 at 05:12
  • I do not understand, why do we need to add font-awesome via npm, couldn't we just link to font-awesome cdn? What am I missing? – Rosdi Kasim Jan 13 '17 at 05:38
  • 3
    @Rosdi Kasim Jan - you could do that - but in bigger apps generally you don't use cdns. First because somebody can hack them and compromise your app indirectly. Second because it's not just font-awesome - you need other external libraries like bootstrap, dnd libraries etc - if you have separate http request - to a cdn - for each of them, it's bad performance. What you do instead, is download with npm - and bundle with all your code in one single file using webpack or something, minify and uglify that -- and that's how you can run your app on mobile -- where the resources are low. – AIon Feb 03 '17 at 17:32
  • 12
    Note: the `addons` property is no longer used. It's sufficient to include the `font-awesome.css` file under `styles`. See https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/include-font-awesome.md – MiKo Feb 13 '17 at 10:37
  • @AIon now "addons" property is removed from angular-cli. Now when I build the app, it is unable to load the .woff2, woff and ttf files. – indra257 Apr 12 '17 at 17:49
  • 1
    @indra257 i've updated the answer, by removing the `addons`. - @0X2D9A3 says it's ***sufficient to include the font-awesome.css in the styles array*** and lot's of people pointing to that being right. Please make sure you got everything as described. I didn't test it personally - I mostly use elm this days, a much better experience. – AIon Apr 13 '17 at 23:15
  • 1
    It runs fine when I do ng serve. When I do ng build it runs fine too but the console shows it is unable to load tff, woff and woff2 files. I tested this with an empty app and just included font-awesome – indra257 Apr 14 '17 at 00:29
  • @indra257 i've digged down into the Angular source code - `WebpackResourceLoader` - here https://github.com/angular/angular-cli/blob/master/packages/%40ngtools/webpack/src/resource_loader.ts#L114, and i think what the problem is: cli uses `UTF-8` encoding to read the files.. and from what i understand here - http://stackoverflow.com/questions/13372997/why-node-js-failed-to-serve-woff-files - the `woff2`, `woff`, and `tff` need to be read as `binary`, not `utf-8`. Not sure what to do about it. Angular folks need to fix this - there is no setting for now, except changing the source code. – AIon Apr 14 '17 at 11:40
  • @AIon Can you suggest any workaround. I think I can't deploy my application with errors in the console. – indra257 Apr 14 '17 at 13:12
  • @indra257 i would clone the repo and modify the Angular source code, like this: take out the extension form the `filePath` argument which is passed into the `get` function as string, check if the extension is `woff2` or `woff` or `tff`. If True `return this._compile(filePath, readFileSync(filePath, 'binary'))` - else leve it as it is: `return this._compile(filePath, readFileSync(filePath, 'utf8'))`. Not complicated. Leave the `.then` branch inside both intact. But i say all this of the top of my head. Didn't test it. Usually It's bad to do this because you loose it on the next Angular update. – AIon Apr 14 '17 at 13:34
  • @Alon Side question - are you using Bootstrap with Material ? I noticed `../node_modules/bootstrap/dist/css/bootstrap.css` – bhantol Jun 12 '17 at 14:54
  • In angular 4, this is not working if i try to build it with ng build -prod, and than when in prod version of the code, font-awesome still show as square. – user2412555 Sep 24 '17 at 07:53
  • @ user2412555 i'm trying to understand your situation. Does it work on dev but not on production? are there any errors/warnings in the console? Thanks. – AIon Sep 24 '17 at 15:46
  • Could you get me some help with [this post](https://stackoverflow.com/questions/48528476/webpack-loading-iconic-fonts) – Jordi Jan 31 '18 at 09:44
  • 4
    Update: With Angular 6.0.0 the filename is changed from .angular-cli.json to angular.json – Manoj Singh Sep 10 '18 at 05:27
  • 1
    I had to change the css path to `node_modules/font-awesome/css/font-awesome.min.css`. See that i am pointing to the min file (more efficient). – Juan Ignacio Avendaño Huergo Mar 20 '20 at 20:33
  • Be careful that you are using the right version of font-awesome from npm, as the font you are trying to copy over. I think this wasn't working, but then realized I was using a different version of font-awesome. – Daniel Viglione Mar 27 '20 at 17:48
  • 1
    This answer is outdated, this installs FA version 4.7, [read this](https://stackoverflow.com/a/52456022/3596441) – The One Oct 09 '20 at 19:23
  • Angular 9+ supports **`ng add @fortawesome/angular-fontawesome`**. See [this](https://stackoverflow.com/a/46596735/11457981) answer. – Abdulwehab Apr 27 '23 at 19:34
141

If you are using SASS, you can just install it via npm

npm install font-awesome --save

and import it in your /src/styles.scss with:

$fa-font-path: "../node_modules/font-awesome/fonts";
@import "../node_modules/font-awesome/scss/font-awesome.scss";

Tip: Whenever possible, avoid to mess with angular-cli infrastructure. ;)

F.D.Castel
  • 2,284
  • 1
  • 16
  • 15
  • 1
    It's the best solution imo. Works like a charm after a `ng build && ng serve -w`. It's easier and safer to let scss build style + fonts than trying to change angular-cli infra ;) – soywod Dec 05 '16 at 15:47
  • 38
    Best answer. Minor improvement: use `~` instead of `../node_modules/`, e.g. `@import '~font-awesome/scss/font-awesome.scss';` – m4js7er Mar 12 '17 at 18:43
  • 5
    not working for me with angular4 and scss. I can see the css but not the icon – Aniruddha Das Apr 16 '17 at 12:44
  • I have same problem on Angular4 – Francesco Jun 19 '17 at 13:18
  • 2
    Use the `.css` import from `~font-awesome/css/font-awesome.min.css` and it works fine (default fa-font-path) for angular4/cli – Tim Aug 24 '17 at 07:31
  • @F.D.Castel +1 for the avoid messing with the `angular-cli` infrastructure :-) – reckface Apr 20 '18 at 16:12
77

The top answer is a bit outdated and there is a slightly easier way.

  1. install through npm

    npm install font-awesome --save

  2. in your style.css:

    @import '~font-awesome/css/font-awesome.css';

    or in your style.scss:

    $fa-font-path: "~font-awesome/fonts"; @import '~font-awesome/scss/font-awesome';

    Edit: as noted in the comments the line for fonts must be changed on newer versions to $fa-font-path: "../../../node_modules/font-awesome/fonts";

using the ~ will make sass look into node_module. It's better to do it this way than with the relative path. The reason is that if you upload a component on npm, and you import font-awesome inside the component scss then it will work properly with ~ and not with the relative path that will be wrong at that point.

This method works for any npm module that contains css. It works for scss as well. However if you are importing css into your styles.scss it won't work (and maybe vice versa). Here is why

Ced
  • 15,847
  • 14
  • 87
  • 146
  • Still getting the same error after following your steps. – indra257 Apr 12 '17 at 17:57
  • Failed to load .ttf, woff and woff2 files – indra257 Apr 13 '17 at 13:09
  • I am using cli 1.0 . Just to double check, I created one sample app and just loaded font-awesome and deployed the app. still getting that error. – indra257 Apr 13 '17 at 18:28
  • @indra257 I had to add $fa-font-path: "../node_modules/font-awesome/fonts"; in styles.scss for above instructions to work – Todilo Apr 24 '17 at 18:22
  • I dont have style.scss file in my project. Do I have to follow any other steps to after adding styles.scss file. – indra257 Apr 24 '17 at 18:36
  • I was trying this, but was getting warnings during the build process: `unable to find path user/dev/~font-awesome....`. The problem was using the `~` with the `$fa-font-path`, if I used a relative path instead of the `~` here it worked fine. Maybe the tilde is not suppose to be used for paths other than imports? – Drenai Jan 08 '18 at 18:49
  • @Brian I'm not sure, it worked for me so far, maybe a new version made it not work – Ced Jan 09 '18 at 13:47
60

There are 3 parts to using Font-Awesome in Angular Projects

  1. Installation
  2. Styling (CSS/SCSS)
  3. Usage in Angular

Installation

Install from NPM and save to your package.json

npm install --save font-awesome

Styling If using CSS

Insert into your style.css

@import '~font-awesome/css/font-awesome.css';

Styling If using SCSS

Insert into your style.scss

$fa-font-path: "../node_modules/font-awesome/fonts";
@import '~font-awesome/scss/font-awesome.scss';

Usage with plain Angular 2.4+ 4+

<i class="fa fa-area-chart"></i>

Usage with Angular Material

In your app.module.ts modify the constructor to use the MdIconRegistry

export class AppModule {
  constructor(matIconRegistry: MatIconRegistry) {
    matIconRegistry.registerFontClassAlias('fontawesome', 'fa');
  }
}

and add MatIconModule to your @NgModule imports

@NgModule({
  imports: [
    MatIconModule,
      ....
  ],
  declarations: ....
}

Now in any template file you can now do

<mat-icon fontSet="fontawesome" fontIcon="fa-area-chart"></mat-icon>
muttonUp
  • 6,351
  • 2
  • 42
  • 54
  • 1
    Do you have any problem with bundling the font-awesome. I think the main issue is bundling when we use Cli. tff, woff, woff2 files are not getting bundled. – indra257 Apr 13 '17 at 14:56
  • I just created a sample application and followed your steps. Bundled the app using ng build. In the console I am still seeing Unable to load woff and woff2 files error – indra257 Apr 13 '17 at 15:15
  • 1
    That's correct. It works perfectly fine with ng serve. With ng build, it works fine, but console shows it is unable to load some woff, woff2 files. – indra257 Apr 13 '17 at 18:30
  • I don't see the same problem, so I expect something is wrong with your build/config files. I suggest you replicate your problem with a test case and create a new question with it. – muttonUp Apr 13 '17 at 18:40
  • I created an empty app by using angular-cli and added font-awesome. Which files do you want me to check.. – indra257 Apr 13 '17 at 18:42
50

UPDATE Feb 2020:
fortawesome package now supports ng add but it is available only for angular 9 and up:

ng add @fortawesome/angular-fontawesome

UPDATE 8 Oct 2019:

You can use a new package https://www.npmjs.com/package/@fortawesome/angular-fontawesome

npm install @fortawesome/angular-fontawesome @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons

Add FontAwesomeModule to imports in src/app/app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
 
import { AppComponent } from './app.component';
import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
 
@NgModule({
  imports: [
    BrowserModule,
    FontAwesomeModule
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

Tie the icon to the property in your component src/app/app.component.ts:

import { Component } from '@angular/core';
import { faCoffee } from '@fortawesome/free-solid-svg-icons';
 
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {
  faCoffee = faCoffee;
}

Use the icon in the template src/app/app.component.html:

<fa-icon [icon]="faCoffee"></fa-icon>

ORIGINAL ANSWER:

Option 1:

You can use angular-font-awesome npm module

npm install --save font-awesome angular-font-awesome

Import the module:

...
//
import { AngularFontAwesomeModule } from 'angular-font-awesome';
@NgModule({
  //...
  imports: [
    //...
    AngularFontAwesomeModule
  ],
  //...
})
export class AppModule { }

If you're using Angular CLI, add the font-awesome CSS to styles inside the angular-cli.json

"styles": [
    "styles.css",
    "../node_modules/font-awesome/css/font-awesome.css"
],

NOTE: If using SCSS preprocessor just change the css for scss

Example Use:

<fa name="cog" animation="spin"></fa>

Option 2:

There is an official story for that now

Install the font-awesome library and add the dependency to package.json

npm install --save font-awesome

Using CSS

To add Font Awesome CSS icons to your app...

// in .angular-cli.json

"styles": [
  "styles.css",
  "../node_modules/font-awesome/css/font-awesome.css"
]

Using SASS

Create an empty file _variables.scss in src/.

Add the following to _variables.scss:

$fa-font-path : '../node_modules/font-awesome/fonts'; In styles.scss add the following:

@import 'variables';
@import '../node_modules/font-awesome/scss/font-awesome';

Test

Run ng serve to run your application in develop mode, and navigate to http://localhost:4200.

To verify Font Awesome has been set up correctly, change src/app/app.component.html to the following...

<h1>
  {{title}} <i class="fa fa-check"></i>
</h1>

After saving this file, return to the browser to see the Font Awesome icon next to the app title.

Also there is a related question Angular CLI outputs the font-awesome font files the dist root as by default angular cli outputs the fonts in to the dist root, which is by the way not an issue at all.

angularrocks.com
  • 26,767
  • 13
  • 87
  • 104
  • When using option 1, remove `../` from in front of `"../node_modules/font-awesome/css/font-awesome.css"` – Alf Moh Oct 20 '19 at 17:08
  • WHY am I supposed to use the new angular package? It seems way more of a PITA to import them individually. – MDave Jan 27 '20 at 05:56
  • @MDave you do not have to, there is plenty of options how to use bootstrap in angular apps. – angularrocks.com Jan 29 '20 at 12:09
  • just a quick note that with the most recent option you still need to `npm install @fortawesome/angular-fontawesome`, might be worth adding this to the answer – Preston Feb 03 '20 at 11:14
  • @User632716 it is there, on the first line – angularrocks.com Feb 03 '20 at 12:25
  • 1
    I mean that `npm install @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons` in your answer is not equal to `npm install @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons @fortawesome/angular-fontawesome` in the docs, apologies if im missing something obvious. – Preston Feb 03 '20 at 12:35
  • 1
    @User632716 OK just added the missing package to npm install command. – angularrocks.com Feb 03 '20 at 21:08
  • 2
    Thanks a lot... I was searching for Angular 9 + font awesome. UPDATE Feb 2020 is the most helpful thing after wasting 1 day. – Tejashree Apr 01 '20 at 13:04
  • 2
    Thanks for angular 9+ font awesome update, it worked for me – Rahul.S Oct 27 '20 at 00:47
  • The update is also awesome. Thank you. – Abdulwehab Apr 27 '23 at 19:39
20

Accepted answer is outdated.

For angular 9 and Fontawesome 5

  1. Install FontAwesome

    npm install @fortawesome/fontawesome-free --save

  2. Register it on angular.json under styles

    "node_modules/@fortawesome/fontawesome-free/css/all.min.css"

  3. Use it on your application

Ron Michael
  • 1,381
  • 12
  • 15
17

Thought I would throw in my resolution to this since font-awesome is now installed differently according to their documentation.

npm install --save-dev @fortawesome/fontawesome-free

Why it is fortawesome now escapes me but thought I would stick with the most recent version rather than falling back to the old font-awesome.

Then I imported it into my scss

$fa-font-path: "../node_modules/@fortawesome/fontawesome-free/webfonts";
@import "~@fortawesome/fontawesome-free/scss/fontawesome";
@import "~@fortawesome/fontawesome-free/scss/brands";
@import "~@fortawesome/fontawesome-free/scss/regular";
@import "~@fortawesome/fontawesome-free/scss/solid";
@import "~@fortawesome/fontawesome-free/scss/v4-shims";

Hope this helps

Nabel
  • 1,767
  • 14
  • 23
16

With Angular2 RC5 and angular-cli 1.0.0-beta.11-webpack.8 you can achieve this with css imports.

Just install font awesome:

npm install font-awesome --save

and then import font-awesome in one of your configured style files:

@import '../node_modules/font-awesome/css/font-awesome.css';

(style files are configured in angular-cli.json)

shusson
  • 5,542
  • 34
  • 38
  • 1
    Looks like it is trying to import but getting an error `zone.js:101 - GET http://localhost:4200/node_modules/font-awesome/css/font-awesome.css 404 (Not Found) `... file actually exists but looks like `localhost:4200` is not running from the root of this folder ... How to package up font-awesome to `localhost:4200` root folder ... – microchip78 Sep 08 '16 at 20:25
  • also I am using `angular-cli@1.0.0-beta.11-webpack.2` and style file configuration in `angular-cli.json` is not working ... – microchip78 Sep 08 '16 at 20:29
  • 1
    hmm that is strange, maybe upgrade to `1.0.0-beta.11-webpack.8` ? – shusson Sep 08 '16 at 22:27
7

Many instructions above work, I suggest looking at those. However, something to note:

Using <i class="fas fa-coffee"></i> did not work in my project (new practice project only a week old) after installation and the sample icon here was also copied to the clipboard from Font Awesome within the past week.

This<i class="fa fa-coffee"></i> does work. If after installing Font Awesome on your project it isn't yet working, I suggest checking the class on your icon to remove the 's' to see if it works then.

mckenna
  • 159
  • 1
  • 7
7

For fontawesome 5.x+ the most simplest way would be the following,

install using npm package: npm install --save @fortawesome/fontawesome-free

In your styles.scss file include:

$fa-font-path: "~@fortawesome/fontawesome-free/webfonts";
@import '~@fortawesome/fontawesome-free/scss/fontawesome';
@import '~@fortawesome/fontawesome-free/scss/solid';
@import '~@fortawesome/fontawesome-free/scss/regular';

Note: if you have _variables.scss file then it's more appropriate to include the $fa-font-path inside it and not in styles.scss file.

5

For Angular 6,

First npm install font-awesome --save

Add node_modules/font-awesome/css/font-awesome.css to angular.json.

Remember not to add any dots in front of the node_modules/.

Alf Moh
  • 7,159
  • 5
  • 41
  • 50
5

This post describes how to integrate Fontawesome 5 into Angular 6 (Angular 5 and previous versions will also work, but then you have to adjust my writings)

Option 1: Add the css-Files

Pro: Every icon will be included

Contra: Every icon will be included (bigger app size because all fonts are included)

Add the following package:

npm install @fortawesome/fontawesome-free-webfonts

Afterwards add the following lines to your angular.json:

"app": {
....
"styles": [
....
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fontawesome.css",
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fa-regular.css",
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fa-brands.css",
"node_modules/@fortawesome/fontawesome-free-webfonts/css/fa-solid.css"
],
...    
}

Option 2: Angular package

Pro: Smaller app size

Contra: You have to include every icon you want to use seperatly

Use the FontAwesome 5 Angular package:

npm install @fortawesome/angular-fontawesome

Just follow their documentation to add the icons. They use the svg-icons, so you only have to add the svgs / icons, you really use.

Paul
  • 2,430
  • 3
  • 15
  • 20
  • Notice your paths start with @fortawesome. Notice the word 'FORT', not 'FONT' My install is doing this too. Anyone know what is up with this? – Post Impatica Jul 26 '18 at 19:54
  • Never mind, apparently they have some renaming structuring going on. See [This Github Post](https://github.com/FortAwesome/Font-Awesome/issues/2861) – Post Impatica Jul 26 '18 at 20:00
  • @Aniketkale what is not working? I presented two options – Paul Jan 09 '19 at 14:38
4

There are many good answers here. But if you tried all of them and still getting squares instead fontawesome icons, check your css rules. In my case I had the following rule:

* {
  font-family: Roboto-Light, Roboto, 'Helvetica Neue', sans-serif !important;
}

And it overrides fontawesome fonts. Just replacing * selector to body solved my problem:

body {
  font-family: Roboto-Light, Roboto, 'Helvetica Neue', sans-serif !important;
}
P.S.
  • 15,970
  • 14
  • 62
  • 86
  • Thank you so much that did it for me granted now I have to figure out how to get the fonts working since doing it in a body selector doesn't work for me – jgerstle Oct 24 '17 at 16:17
  • @jgerstle, specifying font in `body` should work, be sure, that you don't override the font somewhere else, may be you overriding it in `h*` or `p` tags as we are usually do. – P.S. Oct 24 '17 at 18:48
  • Ya something seems to be overriding it but I don't think it's my own code I think it might just be the chrome defaults it's weird though because I set it to !important and it still seems to get overridden. I'll have to look more into it. – jgerstle Oct 24 '17 at 18:54
  • I got it using :not(.fa) to keep the same * selector, but not target anything using font-awesome – jgerstle Oct 25 '17 at 05:56
3

After some experimentation I managed to get the following working:

  1. Install with npm:

    npm install font-awesome --save
    
  2. add to angular-cli-build.js file:

    vendorNpmFiles : [
        font-awesome/**/*.+(css|css.map|otf|eot|svg|ttf|woff|woff2)',
    ]
    
  3. add to index.html

    <link rel="stylesheet" href="vendor/font-awesome/css/font-awesome.min.css">
    

The key was to include the font file types in the angular-cli-build.js file

.+(css|css.map|otf|eot|svg|ttf|woff|woff2)

iSkore
  • 7,394
  • 3
  • 34
  • 59
Nik
  • 7,086
  • 6
  • 36
  • 52
  • 1
    just a heads up there is no `angular-cli-build.js` in the latest webpack branch – shusson Sep 14 '16 at 01:50
  • Also, [vendorNpmFiles is not found in the current Angular-CLI code](https://github.com/angular/angular-cli/search?type=Code&q=vendorNpmFiles). Seeing Alon's accepted answer, I guess this is outdated? – Arjan Nov 02 '16 at 14:11
  • 1
    @Arjan yep, this answer is outdated...this was for the pre webpack CLI. Alon's answer is the accepted answer – Nik Nov 02 '16 at 17:48
2

Edit: I'm using angular ^4.0.0 and Electron ^1.4.3

If you have issues with ElectronJS or similar and have a sort of 404 error, a possible workaround is to uedit your webpack.config.js, by adding (and by assuming that you have the font-awesome node module installed through npm or in the package.json file):

new CopyWebpackPlugin([
     { from: 'node_modules/font-awesome/fonts', to: 'assets' },
     { from: 'src/assets', to: 'assets' }
]),

Note that the webpack configuration I'm using has src/app/dist as output, and, in dist, an assets folder is created by webpack:

// our angular app
entry: {
    'polyfills': './src/polyfills.ts',
    'vendor': './src/vendor.ts',
    'app': './src/app/app',
},

// Config for our build files
output: {
    path: helpers.root('src/app/dist'),
    filename: '[name].js',
    sourceMapFilename: '[name].map',
    chunkFilename: '[id].chunk.js'
},

So basically, what is currently happening is:

  • Webpack is copying the fonts folder to the dev assets folder.
  • Webpack is copying the dev assets folder to the dist assets folder

Now, when the build process will be finished, the application will need to look for the .scss file and the folder containing the icons, resolving them properly. To resolve them, I've used this in my webpack config:

// support for fonts
{
    test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
    loader: 'file-loader?name=dist/[name]-[hash].[ext]'
},

Finally, in the .scss file, I'm importing the font-awesome .scss and defining the path of the fonts, which is, again, dist/assets/font-awesome/fonts. The path is dist because in my webpack.config the output.path is set as helpers.root('src/app/dist');

So, in app.scss:

$fa-font-path: "dist/assets/font-awesome/fonts";
@import "~font-awesome/scss/font-awesome.scss";

Note that, in this way, it will define the font path (used later in the .scss file) and import the .scss file using ~font-awesome to resolve the font-awesome path in node_modules.

This is quite tricky, but it's the only way I've found to get around the 404 error issue with Electron.js

briosheje
  • 7,356
  • 2
  • 32
  • 54
2

Starting from https://github.com/AngularClass/angular-starter, after having tested a lot of different configuration combination, here is what I did to get it working with AoT.

As already said many times, in my app.component.scss:

$fa-font-path: "~font-awesome/fonts" !default;
@import "~font-awesome/scss/font-awesome";

Then in webpack.config.js (actually webpack.commong.js in the starter pack) :

In the plugins section:

new CopyWebpackPlugin([
    { from: 'src/assets', to: 'assets' },
    { from: 'src/meta'},
    { from: 'node_modules/font-awesome/fonts', to: 'assets/fonts/' }
]),

In the rules section:

,
{
    test: /\.(eot|woff2?|svg|ttf)([\?]?.*)$/,
    use: 'file-loader?name=/assets/fonts/[name].[ext]'
}
user3582315
  • 201
  • 3
  • 8
2

I wasted several hours trying to get the latest version of FontAwesome 5.2.0 working with AngularCLI 6.0.3 and Material Design. I followed the npm installation instructions off of the FontAwesome website

Their latest docs instruct you do install using the following:

npm install @fortawesome/fontawesome-free

After wasting several hours I finally uninstalled it and installed font awesome using the following command (this installs FontAwesome v4.7.0):

npm install font-awesome --save

Now it's working fine using:

$fa-font-path: "~font-awesome/fonts" !default;
@import "~font-awesome/scss/font-awesome.scss";
<mat-icon fontSet="fontawesome" fontIcon="fa-android"></mat-icon>
Post Impatica
  • 14,999
  • 9
  • 67
  • 78
2

You can use Angular Font Awesome package

npm install --save font-awesome angular-font-awesome

and then import in your module:

import { AngularFontAwesomeModule } from 'angular-font-awesome';
     @NgModule({
       //...
      imports: [
        //...
        AngularFontAwesomeModule
      ],
      //...
    })
    export class AppModule { }

and import the style in angular-cli file:

   "styles": [
        "styles.css",
        "../node_modules/font-awesome/css/font-awesome.css"
    ],

see more details about the package in npm library:

https://www.npmjs.com/package/angular-font-awesome

and then use it like this:

<i class="fa fa-coffee"></i>

Shmulik
  • 134
  • 2
  • 10
2

Font Awesome gives you scalablevector icons that can instantly be customized—size, color, drop shadow, and anything that can be done with the power of CSS.

Create a new project and navigate into the project.

ng new navaApp
cd navaApp

Install the font-awesome library and add the dependency to package.json.

npm install --save font-awesome

Using CSS

To add Font Awesome CSS icons to your app...

// in angular.json
"build": {
"options": {
"styles": [
  "../node_modules/font-awesome/css/font-awesome.css",
  "src/styles.css"
],
 }
}

Using SASS

Create new project with SASS:

ng new cli-app --style=scss

To use with existing project with CSS:

Rename src/styles.css to src/styles.scss Change angular.json to look for styles.scss instead of css:

// in angular.json
"build": {
"options": {
"styles": [
  "src/styles.scss"
],
}
}

Make sure to change styles.css to styles.scss.

Create an empty file _variables.scss in src/.

Add the following to _variables.scss:

$fa-font-path : '../node_modules/font-awesome/fonts';

In styles.scss add the following:

@import 'variables';
@import '../node_modules/font-awesome/scss/font-awesome';
Mojtaba Nava
  • 858
  • 7
  • 17
2

To use Font Awesome 5 in your Angular project, insert the code below in the of the src/index.html file.

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">

Good luck!

Cherma Ramalho
  • 373
  • 3
  • 7
1

Using LESS (not SCSS) and Angular 2.4.0 and standard Webpack (not Angular CLI, the following worked for me:

npm install --save font-awesome

and (in my app.component.less):

@import "~font-awesome/less/font-awesome.less";

and of course you may need this obvious and highly intuitive snippet (in module.loaders in webpack.conf)

        {
            test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)(\?(v=)?(\d+)(\.\d+)*)?$/,
            loader: 'file?name=graphics/[name].[ext]'
        },

The loader is there to fix webpack errors of the kind

"Module parse failed: \node_modules\font-awesome\fonts\fontawesome-webfont.svg?v=4.7.0 Unexpected token (1:0)" 

and the regexp matches those svg-references (with or without version specification). Depending on your webpack setup you might not need it or you may need something else.

dpnmn
  • 493
  • 2
  • 7
1

For webpack2 use:

{
 test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)(\?(v=)?(\d+)(\.\d+)*)?$/,
                    loader: "file-loader"
}

instead of file-loader?name=/assets/fonts/[name].[ext]

aristotll
  • 8,694
  • 6
  • 33
  • 53
1

Add it in your package.json as "devDependencies" font-awesome : "version number"

Go to Command Prompt type npm command which you configured.

  • You'll want to add font-awesome to your 'dependencies', not 'dev-dependencies' as you'll need it in your final build. Also the instructions above do not answer how the Angular project will pick it up once they are added to you package.json file. – Nik Jan 19 '18 at 13:16
1

I wanted to use Font Awesome 5+ and most answers focus on older versions

For the new Font Awesome 5+ the angular project hasn't been released yet, so if you want to make use of the examples mentioned on the font awesome website atm you need to use a work around. (especially the fas, far classes instead of the fa class)

I've just imported the cdn to Font Awesome 5 in my styles.css. Just added this in case it helps someone find the answer quicker than I did :-)

Code in Style.css

@import "https://use.fontawesome.com/releases/v5.0.7/css/all.css";
1

If for some reason you cant install package throw npm. You can always edit index.html and add font awesome CSS in the head. And then just used it in the project.

Semir Hodzic
  • 509
  • 6
  • 9
1

For Angular 9 using ng :

ng add @fortawesome/angular-fontawesome@0.6.x

Look Here For More Information

M Fuat
  • 1,330
  • 3
  • 12
  • 24
1

Now there is few ways to install fontAwesome on Angular CLI:

ng add @fortawesome/angular-fontawesome

OR using yarn

yarn add @fortawesome/fontawesome-svg-core
yarn add @fortawesome/free-solid-svg-icons
yarn add @fortawesome/angular-fontawesome

OR Using NPM

npm install @fortawesome/fontawesome-svg-core
npm install @fortawesome/free-solid-svg-icons
npm install @fortawesome/angular-fontawesome

Reference here: https://github.com/FortAwesome/angular-fontawesome

Archil Labadze
  • 4,049
  • 4
  • 25
  • 42
1

In Angular 11

npm install @fortawesome/angular-fontawesome --save
npm install @fortawesome/fontawesome-svg-core --save
npm install @fortawesome/free-solid-svg-icons --save

And then in app.module.ts at imports array

import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';

  imports: [
    BrowserModule,
    FontAwesomeModule
  ],

And then in any.component.ts

turningGearIcon = faCogs;

And then any.component.html

<fa-icon [icon]="turningGearIcon"></fa-icon>
Malik Haseeb
  • 471
  • 9
  • 20
1

If you want to use free version of font awesome - bootstrap, use this :

npm install --save @fortawesome/fontawesome-free

If you are building angular project, you also need to add these imports in your angular.json :

"styles": [
          "src/styles.css",
          "node_modules/bootstrap/dist/css/bootstrap.min.css",
          "node_modules/@fortawesome/fontawesome-free/css/fontawesome.min.css"
        ],
        "scripts": [
          "node_modules/jquery/dist/jquery.min.js",
          "node_modules/bootstrap/dist/js/bootstrap.min.js",
          "node_modules/popper.js/dist/umd/popper.min.js",
          "node_modules/@fortawesome/fontawesome-free/js/all.js"
        ]
ASK
  • 1,136
  • 1
  • 10
  • 14