如何将Font Awesome添加到Next.js 13项目中出现的模块未找到错误

huangapple go评论62阅读模式
英文:

How to add Font Awesome to Next.js 13 project error Module not found

问题

我想将Font Awesome添加到我的Next.js 13项目中。我尝试实现解决方案#3,这是很久以前提出的问题。我还尝试了FontAwesome文档中的解决方案。我刚刚开始使用Next.js,一开始我按照文档的步骤进行操作,但出现了“模块未找到”的错误。然后我尝试修改这些解决方案,使其适应版本13中的新更改。供参考,我的项目使用“/src/app/pages.tsx”文件结构。我尝试添加了“/src/app/_app.tsx”并包含以下内容:

import { config, library } from '@fortawesome/fontawesome-svg-core';
import { fas } from '@fortawesome/free-solid-svg-icons';
import '@fortawesome/fontawesome-svg-core/styles.css';
config.autoAddCss = false;
library.add(fas);

在我的“/src/app/page.tsx”中,我添加了导入并尝试使用FontAwesome:

import Image from 'next/image';
import Link from "next/link";
import cardStyles from './styles/card.module.css';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';

export default function Home() {

  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
        <FontAwesomeIcon icon={['fab', 'linkedin']} />LinkedIn
    </main>
  )
}

但我一直收到相同的错误:

./src/app/page.tsx:4:0
Module not found: Can't resolve '@fortawesome/react-fontawesome'
  2 | import Link from "next/link";
  3 | import cardStyles from './styles/card.module.css';
> 4 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
  5 | 
  6 | 
  7 | export default function Home() {

我在package.json中有这个包,并且已经安装在我的node_modules中:

"dependencies": {
    "@fortawesome/fontawesome-svg-core": "^6.4.0",
    "@fortawesome/free-brands-svg-icons": "^6.4.0",
    "@fortawesome/free-solid-svg-icons": "^6.4.0",
    "@fortawesome/react-fontawesome": "^0.2.0",
}

我甚至重新安装了它以确保。我不确定如何排除此错误。是否可能是因为pages.tsx正在进行SSR而不是在客户端上执行?

英文:

I would like to add fontawesome to my nextjs 13 project. I'm trying to implement solution #3 which was asked a long time ago. I've tried the solution in the fontawesome documentation as well. I'm just starting out with nextjs and at first I followed the documentation, but that produced a Module not found error. Then I tried to modify these solutions to work with the new changes in version 13. For reference my project is using the /src/app/pages.tsx file structure. I tried adding /src/app/_app.tsx with the following content:

import { config, library } from &#39;@fortawesome/fontawesome-svg-core&#39;;
import { fas } from &#39;@fortawesome/free-solid-svg-icons&#39;;
import &#39;@fortawesome/fontawesome-svg-core/styles.css&#39;;
config.autoAddCss = false;
library.add(fas);

In my /src/app/page.tsx I added the import and tried to use fontawesome:

import Image from &#39;next/image&#39;
import Link from &quot;next/link&quot;;
import cardStyles from &#39;./styles/card.module.css&#39;
import { FontAwesomeIcon } from &#39;@fortawesome/react-fontawesome&#39;

export default function Home() {

  return (
    &lt;main className=&quot;flex min-h-screen flex-col items-center justify-between p-24&quot;&gt;
        &lt;FontAwesomeIcon icon={[&#39;fab&#39;, &#39;linkedin&#39;]} /&gt;LinkedIn
    &lt;/main&gt;
  )
}

and I keep getting the same error:

./src/app/page.tsx:4:0
Module not found: Can&#39;t resolve &#39;@fortawesome/react-fontawesome&#39;
  2 | import Link from &quot;next/link&quot;;
  3 | import cardStyles from &#39;./styles/card.module.css&#39;
&gt; 4 | import { FontAwesomeIcon } from &#39;@fortawesome/react-fontawesome&#39;
  5 | 
  6 | 
  7 | export default function Home() {

I do have the package in my package.json and installed in my node_modules:

  &quot;dependencies&quot;: {
    &quot;@fortawesome/fontawesome-svg-core&quot;: &quot;^6.4.0&quot;,
    &quot;@fortawesome/free-brands-svg-icons&quot;: &quot;^6.4.0&quot;,
    &quot;@fortawesome/free-solid-svg-icons&quot;: &quot;^6.4.0&quot;,
    &quot;@fortawesome/react-fontawesome&quot;: &quot;^0.2.0&quot;,

I even reinstalled it to be sure. I'm not sure how to troubleshoot this error. Could it be caused because the pages.tsx is being SSR instead of being done on client side?

答案1

得分: 1

RootLayout中:

import "@fortawesome/fontawesome-svg-core/styles.css";
import { config } from "@fortawesome/fontawesome-svg-core";
config.autoAddCss = false;

然后在Home页面:

import Image from "next/image";
import styles from "./page.module.css";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCheck } from "@fortawesome/free-solid-svg-icons";

export default function Home() {
  return (
    <main className={styles.main}>
      <FontAwesomeIcon
        icon={faCheck}
        className="fas fa-check"
        style={{ color: "red", fontSize: 64 }}
      />
      Check
    </main>
  );
}

工作证明:

如何将Font Awesome添加到Next.js 13项目中出现的模块未找到错误

@fortawesome/free-solid-svg-icons中不存在Linkedin图标,要使用Linkedin图标,需要执行以下操作:

npm i @fortawesome/free-brands-svg-icons

然后在首页中:

import { faLinkedin } from "@fortawesome/free-brands-svg-icons";

export default function Home() {
  return (
    <main className={styles.main}>
      <FontAwesomeIcon
        icon={faLinkedin}
        style={{ color: "blue", fontSize: 64 }}
      />
      Linkedin
    </main>
  );
}

如何将Font Awesome添加到Next.js 13项目中出现的模块未找到错误

英文:

in RootLayout

import &quot;@fortawesome/fontawesome-svg-core/styles.css&quot;;
import { config } from &quot;@fortawesome/fontawesome-svg-core&quot;;
config.autoAddCss = false;

then in Home page

import Image from &quot;next/image&quot;;
import styles from &quot;./page.module.css&quot;;
import { FontAwesomeIcon } from &quot;@fortawesome/react-fontawesome&quot;;
import { faCheck } from &quot;@fortawesome/free-solid-svg-icons&quot;;

export default function Home() {
  return (
    &lt;main className={styles.main}&gt;
      &lt;FontAwesomeIcon
        icon={faCheck}
        className=&quot;fas fa-check&quot;
        style={{ color: &quot;red&quot;, fontSize: 64 }}
      /&gt;
      Check
    &lt;/main&gt;
  );
}

Proof of work:

如何将Font Awesome添加到Next.js 13项目中出现的模块未找到错误

Linkedin icon does not exist inside @fortawesome/free-solid-svg-icons. to use linkedin

npm i @fortawesome/free-brands-svg-icons 

then in home page

import { faLinkedin } from &quot;@fortawesome/free-brands-svg-icons&quot;;

export default function Home() {
  return (
    &lt;main className={styles.main}&gt;
      &lt;FontAwesomeIcon
        icon={faLinkedin}
        style={{ color: &quot;blue&quot;, fontSize: 64 }}
      /&gt;
      Linkedin
    &lt;/main&gt;
  );
}

如何将Font Awesome添加到Next.js 13项目中出现的模块未找到错误

答案2

得分: 0

<br/>
我遇到了类似的问题,最终使用了ReactIcons。

ReactIcons包括Font Awesome Icons以及其他一些图标,这可能会非常有帮助。请参考此链接,验证是否适合您的需求。
https://react-icons.github.io/react-icons/

英文:

<br/>
I faced with similar issues and endedup using ReactIcons.

ReactIcons consist of Font Awesome Icons along with other icons which can be really helpful. Kindly refer this and validate if these suit your needs.
https://react-icons.github.io/react-icons/

答案3

得分: 0

我建议您将Font Awesome kit复制并粘贴到您的index.html文件的部分,然后您就可以使用所有Font Awesome图标了。

英文:

So I will recommend you to copy fontawesome kit and paste it in <head></head> of your index.html, then you will be able to use all fontawesome icons.

huangapple
  • 本文由 发表于 2023年5月29日 00:09:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定