UPDATE
to set up this in nextjs13
refer to:
How to add Font Awesome to Next.js 13 project error Module not found
There are 3 ways:
1: By Adding a FontAwesome Script to the Head
Component
Create Head
component, add script and render this component either in Header
component or in Layout
file where you use it in every page.
<Head>
<script
// you might need to get a newer version
src="https://kit.fontawesome.com/fbadad80a0.js"
crossOrigin="anonymous"
></script>
</Head>
2: Adding a link to the CDN within the _document.js
.
It's possible to overwrite the root html
file in Next.js by creating a _document.js
under the pages
folder. You could also add some other CDN as well. But, the easiest way is:
<Html>
<Head>
<link
rel="stylesheet"
type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
/>
<link
rel="stylesheet"
type="text/css"
href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
/>
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
3: Install All the Necessary npm
Packages for FontAwesome
npm install --save @fortawesome/fontawesome-svg-core @fortawesome/free-solid-svg-icons @fortawesome/react-fontawesome
And then at the top of the _app.js
file add these lines:
// import Font Awesome CSS
import "@fortawesome/fontawesome-svg-core/styles.css";
import { config } from "@fortawesome/fontawesome-svg-core";
config.autoAddCss = false;
Thereafter, you can import & use the rest of the FontAwesome icons as React Components like so:
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCheck, faTrash } from "@fortawesome/free-solid-svg-icons";
<FontAwesomeIcon icon={faCheck} className="fas fa-check" style={{ color: "red" }}
></FontAwesomeIcon>
@fontawesome/fontawesome-svg-core/styles.css
module not found" error. Haven't verified the other methods though. – Facula