Marcus Lind
Marcus Lind

Reputation: 11470

Angular build can't find installed Font-Awesome node_module

Whenever I build my project using ng build --prod I receive the following error:

ERROR in ./src/styles.scss
Module build failed: 
  ModuleBuildError: Module build failed: 
    Error: Can't resolve '~font-awesome/css/font-awesome.css' in '/app/src'

styles.css uses @import "~font-awesome/css/font-awesome.css";. I've verified the path and that the module is correctly installed at node_modules/font-awesome.

I've also tried importing the scss version with and without file ending. When I remove the @import statement everything works fine, even though I use other imports to other node_modules within the same file -- so it seems to only be an issue with the font-awesome package...

Also, I did SSH in to my docker container to check if node_modules/font-awesome/css/font-awesome.css exists, and it does.

Ideas?

Upvotes: 1

Views: 5460

Answers (3)

Marcus Lind
Marcus Lind

Reputation: 11470

I was not sure why the CSS version of the file never worked, but I was able to find the docs on the angular-cli that gave me some hints to how to get it working.

1) Import the SCSS version instead of the CSS version.

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

2) Define the font path as a SCSS variable.

$fa-font-path: "~font-awesome/fonts";

Note that the SCSS var have to be defined before the @import statement.

Weird thing was that thing error randomly happened. Everything was working fine for one developer, but when pulling the repo to a second computer this issue was introduced.

Upvotes: 2

Nadhir Falta
Nadhir Falta

Reputation: 5267

You need to add that inside your .angular-cli.json file add styles under apps like so:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "MyProject",
    "ejected": false
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "manifest.json",
        {
          "glob": "**/*",
          "input": "../node_modules/leaflet/dist/images",
          "output": "./assets/",
          "allowOutsideOutDir": false
        }
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "serviceWorker": true,
      "styles": [
        "assets/sass/global/loading.css",
        "../node_modules/font-awesome/css/font-awesome.min.css", <===================
        "styles.scss"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "demo": "environments/environment.demo.ts",
        "prod": "environments/environment.prod.ts",
        "qa": "environments/environment.qa.ts",
        "e2e": "environments/environment.e2e.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json",
      "exclude": "**/node_modules/**"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "component": {
    }
  }
}

Upvotes: 0

bahek2462774
bahek2462774

Reputation: 664

that works for me (this line is in my main app.css)

@import "node_modules/font-awesome/scss/font-awesome";

Upvotes: 0

Related Questions