82

I've tried to install Font Awesome using Laravel Mix but when executing run npm dev I get the following message:

ERROR Failed to compile with 1 errors
error in ./~/font-awesome/scss/font-awesome.scss Module build failed: /** ^ Invalid CSS after "...load the styles": expected 1 selector or at-rule, was "var content = requi" in /var/www/html/blog/node_modules/font-awesome/scss/font-awesome.scss (line 1, column 1)

I removed the comments in the file and tried to change font paths, but it did not solve the problem.

webpack.mix.js

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css')
   .copy('node_modules/font-awesome/fonts/', 'public/fonts')
   .sass('node_modules/font-awesome/scss/font-awesome.scss', 'public/css')
   .version();

fontawesome.scss

@import "variables";
@import "mixins";
@import "path";
@import "core";
@import "larger";
@import "fixed-width";
@import "list";
@import "bordered-pulled";
@import "animated";
@import "rotated-flipped";
@import "stacked";
@import "icons";
@import "screen-reader";

_variable.scss

// Variables
// --------------------------

$fa-font-path:        "../fonts" !default;
$fa-font-size-base:   14px !default;
$fa-line-height-base: 1 !default;
// $fa-font-path:        "//netdna.bootstrapcdn.com/font-awesome/4.7.0/fonts" !default; // for referencing Bootstrap CDN font files directly
$fa-css-prefix:       fa !default;
$fa-version:          "4.7.0" !default;
$fa-border-color:     #eee !default;
$fa-inverse:          #fff !default;
$fa-li-width:         (30em / 14) !default;
// Continue...
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Lucas Lopes
  • 1,373
  • 3
  • 14
  • 23
  • Pay attention to the `mix.js('resources/js/app.js', 'public/js') .sass('resources/sass/app.scss', 'public/css');` Part in the question. – M at Feb 11 '21 at 19:08

14 Answers14

130

To install font-awesome you first should install it with npm. So in your project root directory type:

npm install font-awesome --save

(Of course I assume you have node.js and npm already installed. And you've done npm install in your projects root directory)

Then edit the resources/assets/sass/app.scss file and add at the top this line:

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

Now you can do for example:

npm run dev

This builds unminified versions of the resources in the correct folders. If you wanted to minify them, you would instead run:

npm run production

And then you can use the font.

Karl Buys
  • 439
  • 5
  • 12
Kornel
  • 4,184
  • 4
  • 28
  • 30
  • 30
    In Laravel 5.5, this worked for me: ```@import "~font-awesome/scss/font-awesome.scss";``` – Adam Lundrigan Sep 25 '17 at 19:35
  • i should say this helped me since i forgot to update app.scss file but the url is changed , in my situation it is `@import "node_modules/font-awesome-sass/assets/stylesheets/_font-awesome.scss";` – Mohammed Omer Oct 05 '17 at 21:05
  • `yarn` is much faster than `npm` so you can do `yarn add font-awesome --save` – avn Jan 26 '18 at 09:11
  • Once I had a problem with fontawesome. Tried all, downloaded, npm installed. but then found the problem frontend developer wrote a code in css *{} for every tag, and I deleted that code then fontawesome workd perfect – Ozal Zarbaliyev Sep 17 '18 at 11:55
  • 3
    Note that this won't install the latest free version of Font Awesome. It will only install version + font-awesome@4.7.0. – Karl Hill Jan 29 '19 at 23:00
  • don't forget to add `.fa, .fas { font-family: "Font Awesome 5 Brands", "Font Awesome 5 Free", "Font Awesome 5 Pro"; }` if you find out your brand icon is not showing – Jamaluddin Rumi Oct 22 '20 at 09:53
  • for me it works if use `.copy('node_modules/@fortawesome/fontawesome-free/webfonts', 'public/fonts');` at webpack.mix.js then after that `npm run dev` – Yohanim Feb 25 '21 at 09:45
  • ERROR in ./resources/css/app.css Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): ModuleBuildError: Module build failed (from ./node_modules/css-loader/dist/cjs.js): Error: Can't resolve 'node_modules/font-awesome/scss/font-awesome.scss' in '/resources/css' – mercury Jan 05 '22 at 21:43
111

For Font Awesome 5(webfont with css) and Laravel mixin add package for font awesome 5

npm i --save @fortawesome/fontawesome-free

And import font awesome scss in app.scss or your custom scss file

@import '~@fortawesome/fontawesome-free/scss/brands';
@import '~@fortawesome/fontawesome-free/scss/regular';
@import '~@fortawesome/fontawesome-free/scss/solid';
@import '~@fortawesome/fontawesome-free/scss/fontawesome';

compile your assets npm run dev or npm run production and include your compiled css into layout

mfink
  • 1,309
  • 23
  • 32
Karan
  • 2,102
  • 2
  • 22
  • 31
  • 1
    Thank you, it almost worked for me. All I had to do is change the order of lines and move `@import '~@fortawesome/fontawesome-free/scss/fontawesome';` to the top of the imports of font-awesome. – Don40 Feb 08 '19 at 11:01
  • upvoted! do you really need all 4 of these files, i thought ~@fortawesome/fontawesome-free/scss/fontawesome would be sufficient – PirateApp Aug 21 '19 at 09:09
77

How to Install Font Awesome 5 in Laravel 5.3 - 5.6 (The Right Way)

Build your webpack.mix.js configuration.

mix.setPublicPath('public');
mix.setResourceRoot('../');

mix.js('resources/assets/js/app.js', 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

Install the latest free version of Font Awesome via a package manager like npm.

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

This dependency entry should now be in your package.json.

// Font Awesome
"devDependencies": {
    "@fortawesome/fontawesome-free": "^5.15.3",

In your main SCSS file /resources/assets/sass/app.scss, import one or more styles.

@import '~@fortawesome/fontawesome-free/scss/fontawesome';
@import '~@fortawesome/fontawesome-free/scss/regular';
@import '~@fortawesome/fontawesome-free/scss/solid';
@import '~@fortawesome/fontawesome-free/scss/brands';

Compile your assets and produce a minified, production-ready build.

npm run production

Finally, reference your generated CSS file in your Blade template/layout.

<link type="text/css" rel="stylesheet" href="{{ mix('css/app.css') }}">

How to Install Font Awesome 5 with Laravel Mix 6 in Laravel 8 (The Right Way)

https://gist.github.com/karlhillx/89368bfa6a447307cbffc59f4e10b621

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
  • 1
    I also had to include the CSS in /resources/assets/sass/app.scss (prefix import with @): import "~@fortawesome/fontawesome-free/scss/fontawesome.scss"; import "~@fortawesome/fontawesome-free/scss/solid.scss"; – Wotuu Aug 01 '18 at 13:35
  • 7
    I can't get it to work without adding `.copy('node_modules/@fortawesome/fontawesome-free/webfonts', 'public/webfonts')` to my webpack.mix.js – willjohnathan Apr 23 '19 at 05:46
  • 1
    same here, I had to copy webfonts to public, otherwise I get a 404 not found error referring to the webfonts folder.. should update the answer, methinks. – Eleazar Resendez Apr 26 '19 at 15:56
  • in laravel 7.4 the webpack config is already built, thus i have just to `npm` command and then add to `scss` file and then run `npm run whatver-you-want-here` and all done – MR_AMDEV Aug 17 '20 at 14:36
38

This is for new users who are using Laravel 5.7(and above) and Fontawesome 5.3

npm install @fortawesome/fontawesome-free --save

and in app.scss

@import '~@fortawesome/fontawesome-free/css/all.min.css';

Run

npm run watch

Use in layout

<link href="{{ asset('css/app.css') }}" rel="stylesheet">
Ramesh Navi
  • 773
  • 1
  • 11
  • 24
  • 4
    This should be in the top since laravel 5.7 is the current stable build. I've tried all above tips but nothing worked for me except for this one. All I was missing was the link part to put in my header. Thanks alot! :) – wenzzzel Feb 04 '19 at 19:02
  • 1
    This should be accepted answer as the others only work for older versions of laravel – Armin Nov 04 '19 at 17:17
  • Still getting a `/webfonts/fa-solid-900.woff2 net::ERR_ABORTED 404` when trying to load the font using this method. What does the code look like to render the icon on the page? – Alex Dec 19 '19 at 15:55
  • 1
    I also had to copy the font in the webpack.mix.js mix `.copy('node_modules/@fortawesome/fontawesome-free/webfonts', 'public/webfonts')` – Dennis van Dalen Jan 02 '20 at 03:49
  • @Aditya your lucky, its not working for me can you give me fresh laravel 6.*? – Mathew Magante Feb 05 '20 at 12:18
  • It's missing css & font copy syntax in mix file. – Kapil Yadav Jul 03 '20 at 13:44
  • 1
    This worked with the latest version of FA and Laravel 5.5, thanks! – tavernhell Jan 26 '21 at 16:39
16

For Laravel >= 5.5

  • Run npm install font-awesome --save
  • Add @import "~font-awesome/scss/font-awesome.scss"; in resources/assets/saas/app.scss
  • Run npm run dev (or npm run watch or even npm run production)
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
7

I am using Laravel 5.5.14 and none of the above worked for me. So here are the steps I did to make it work.

1.Install font-awesome by using npm:

   npm install font-awesome --save

2.Move the fonts to your public directory by adding this line in your webpack.mixin.js

mix.copyDirectory('node_modules/font-awesome/fonts', 'public/fonts/font-awesome');

3.Open your app.scss to specify the path of the fonts in your node_modules and which relative path to use for compilation:

$fa-font-path: "../fonts/font-awesome" !default;
@import "~font-awesome/scss/font-awesome.scss";
Hyder B.
  • 10,900
  • 5
  • 51
  • 60
  • ```$fa-font-path: "../../../public/fonts/font-awesome" !default;``` This will work as we shall provide relative path to the font directory. –  Jun 06 '18 at 22:21
  • 1
    Why can't we use an absolute path here e.g. ```"/fonts/font-awesome" !default;``` ? – Dr. House Jul 29 '18 at 21:31
  • 1
    Note that this won't install the latest free version of Font Awesome. It will only install version + font-awesome@4.7.0. – Karl Hill Jan 29 '19 at 23:01
5

first install fontawsome using npm

npm install --save @fortawesome/fontawesome-free

add to resources\sass\app.scss

// Fonts
@import '~@fortawesome/fontawesome-free/scss/fontawesome';

and add to resources\js\app.js

require('@fortawesome/fontawesome-free/js/all.js');

then run

npm run dev

or

npm run production
Sakhri Houssem
  • 975
  • 2
  • 16
  • 32
5

Solution for Laravel 7

  1. Install font-awesome library.
npm install font-awesome --save
  1. After installing the library. Open the following file and paste given code

<you_project_location>/resources/sass/app.scss

@import "~font-awesome/scss/font-awesome";
  1. Open the following file and paste given code

<you_project_location>/webpack.mix.js

mix.setResourceRoot("../");
  1. As per your environment execute command.

npm run dev

or

npm run production

There is no need to copy and paste the font folder to the public folder, all will be handled automatically.

Gaurav Porwal
  • 503
  • 3
  • 6
  • 1
    note `mix.setResourceRoot("../");` in this answer, very useful. – abenevaut Aug 08 '20 at 06:35
  • 1
    `mix.setResourceRoot("../");` worked like charm ! Thanks – Emad Saeed Aug 23 '20 at 23:34
  • This should be accepted answer as this is works for the latest version of Laravel, just did a fresh install today. `mix.setResourceRoot("../");` was a lifesaver! @abenevaut and @Emad Saeed should up-vote this answer – Harvey Dobson Oct 30 '20 at 18:29
  • By installing the `font-awesome` package, you're actually missing some of the fonts such as solid. It's better to install `@fortawesome/fontawesome-free` which includes all fonts. – 8ctopus Mar 18 '22 at 05:29
  • This actually installs the old version of font awesome 4.7.0 – 8ctopus Mar 23 '22 at 11:57
5

Laravel 9 and fontawesome 6.1

composer create-project laravel/laravel:^9 laravel-9

cd laravel-9

npm i --save-dev @fortawesome/fontawesome-free@^6.1.0

create file resources/sass/app.scss

@import "@fortawesome/fontawesome-free/scss/fontawesome";
@import "@fortawesome/fontawesome-free/scss/regular";
@import "@fortawesome/fontawesome-free/scss/solid";
@import "@fortawesome/fontawesome-free/scss/brands";

replace webpack.mix.js by

const mix = require('laravel-mix');

// set path to laravel public directory
//mix.setPublicPath('public');

// path to laravel public directory from public/css/app.css
mix.setResourceRoot("../");

mix.js('resources/js/app.js', 'public/js')
    .sass('resources/sass/app.scss', 'public/css');

Add to your view

<script src="{{ mix('js/app.js') }}"></script>
<link rel="stylesheet" href="{{ mix('css/app.css') }}" />

Note: When done correctly, the font files are automatically copied to the public directory. You do not need to copy them manually as seen here in many answers.

Note: If your laravel site is in a subfolder (http://domain/site/), make sure to add mix_url to config/app.php

    // laravel mix assets url
    'mix_url' => env('APP_URL', null),
8ctopus
  • 2,617
  • 2
  • 18
  • 25
4

the path you are using is not correct you could just open the node_module and find the path of font-awesome. use could use js or svg font but i prefer the css style.

at first use this command to install font-awesome-free npm install --save-dev @fortawesome/fontawesome-free

after that you can do this

@import "~@fortawesome/fontawesome-free/scss/fontawesome";
@import "~@fortawesome/fontawesome-free/scss/regular";
@import "~@fortawesome/fontawesome-free/scss/solid";
@import "~@fortawesome/fontawesome-free/scss/brands";

and I copy the font path like below this is optional

.copy('node_modules/@fortawesome/fontawesome-free/webfonts', 'public/fonts');

and finally just run the script npm run dev or npm run watch in laravel

Farshad Fahimi
  • 609
  • 9
  • 20
3

I have recently written a blog about it for the Laravel5.6. Link to blog is https://www.samundra.com.np/integrating-font-awesome-with-laravel-5.x-using-webpack/1574

The steps are similar to above description. But in my case, I had to do extra steps like configuring the webfonts path to font-awesome in "public" directory. Setting the resource root in Laravel mix etc. You can find the details in the blog.

I am leaving the link here so it helps people for whom the mentioned solutions don't work.

Samundra
  • 1,869
  • 17
  • 28
1
npm install font-awesome --save

add ~/ before path

@import "~/font-awesome/scss/font-awesome.scss";
Vahid Alvandi
  • 588
  • 9
  • 17
-1

I found all answers above incomplete somehow, Below are exact steps to get it working.

  1. We use npm in order to install the package. For this open the Console and go to your Laravel application directory. Enter the following:

    npm install font-awesome --save-dev

  2. Now we have to copy the needed files to the public/css and public/fonts directory. In order to do this open the webpack.mix.js file and add the following:

    mix.copy('node_modules/font-awesome/css/font-awesome.min.css', 'public/css'); mix.copy('node_modules/font-awesome/fonts/*', 'public/fonts');

  3. Run the following command in order to execute Laravel Mix:

    npm run dev

  4. Add the stylesheet for the Font Awesome in your applications layout file (resources/views/layouts/app.blade.phpapp.blade.php):

    <link href="{{ asset('css/font-awesome.min.css') }}" rel="stylesheet" />

  5. Use font awesome icons in templates like

    <i class="fa fa-address-book" aria-hidden="true"></i>

I hope it helps!

Kapil Yadav
  • 650
  • 1
  • 5
  • 29
-4

Try in your webpack.mix.js to add the '*'

.copy('node_modules/font-awesome/fonts/*', 'public/fonts')
Dimitri Mostrey
  • 2,302
  • 1
  • 12
  • 11
  • I'm not a scss specialist but it seems it asks where to import from. From the fontawasome site itself you may have to add at line 1 `@fa-font-path: "../font"; It has to be a relative path so in laravel it will probable be "../../../public/font" ` – Dimitri Mostrey Apr 17 '17 at 14:13
  • 1
    Copying is fine, but I feel that importing the `.scss` file into `app.scss` inside of font-awesome works better. – Ru Chern Chong Jun 23 '17 at 15:12
  • Thanks, i think this should be selected as best answer. – Tommix Jun 23 '17 at 21:20