Local fonts fails to resolve in next.js while deploying in vercel.

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

Local fonts fails to resolve in next.js while deploying in vercel

问题

I am trying to use next/fonts in next.js 13.3. Everything works fine locally but when I deployed to Vercel, I keep getting this build error Module not found: Can't resolve './fonts/BrFirma-Thin.woff2'

This is what my folder structure looks like

  • src
    • _app.js
    • styles
      • font.js
      • fonts

_app.js

import { BRFirma } from 'styles/font';
...
 <main className={BRFirma.className}>
  ...
 </main>

font.js

import localFont from 'next/font/local';

export const BRFirma = localFont({
    src: [
       
        {
            path: './fonts/BrFirma-Regular.woff2',
            weight: '400',
        },
        {
            path: './fonts/BrFirma-SemiBold.woff2',
            weight: '600',
        },
    ],
}); 

Again I moved the fonts to the public folder just to make sure I am doing it right.

font.js

import localFont from 'next/font/local';

export const BRFirma = localFont({
    src: [
       
        {
            path: '../../public/assets/fonts/BrFirma-Regular.woff2',
            weight: '400',
        },
        {
            path: '../../public/assets/fonts/BrFirma-SemiBold.woff2',
            weight: '600',
        },
    ],
});

Once again, everything works fine locally but deployment still fails with the module not found message.

Just for the record, this is what jsconfig.js file looks like.

{
    "compilerOptions": {
        "baseUrl": "src",
        "paths": {
            "@/": ["./src/*"]
        }
    },
    "exclude": ["node_modules", "build", "dist", "jest"]
}

(Note: I've removed the HTML entities like &lt; and &quot; to provide a cleaner translation of your code.)

英文:

I am trying to use next/fonts in next.js 13.3. Everything works fine locally but when I deployed to Vercel, I keep getting this build error Module not found: Can&#39;t resolve &#39;./fonts/BrFirma-Thin.woff2&#39;

This is what my folder structure looks like

  • src
    • _app.js
    • styles
      • font.js
      • fonts

_app.js

import { BRFirma } from &#39;styles/font&#39;;
...
 &lt;main className={BRFirma.className}&gt;
  ...
 &lt;/main&gt;

font.js

import localFont from &#39;next/font/local&#39;;

export const BRFirma = localFont({
    src: [
       
        {
            path: &#39;./fonts/BrFirma-Regular.woff2&#39;,
            weight: &#39;400&#39;,
        },
        {
            path: &#39;./fonts/BrFirma-SemiBold.woff2&#39;,
            weight: &#39;600&#39;,
        },
    ],
}); 

Again I moved the fonts to the public folder just to make sure I am doing it right.

font.js

import localFont from &#39;next/font/local&#39;;

export const BRFirma = localFont({
    src: [
       
        {
            path: &#39;../../public/assets/fonts/BrFirma-Regular.woff2&#39;,
            weight: &#39;400&#39;,
        },
        {
            path: &#39;../../public/assets/fonts/BrFirma-SemiBold.woff2&#39;,
            weight: &#39;600&#39;,
        },
    ],
});

Once again, everything works fine locally but deployment still fails with the module not found message.

Just for the record, this is what jsconfig.js file looks like.

{
    &quot;compilerOptions&quot;: {
        &quot;baseUrl&quot;: &quot;src&quot;,
        &quot;paths&quot;: {
            &quot;@/*&quot;: [&quot;./src/*&quot;]
        }
    },
    &quot;exclude&quot;: [&quot;node_modules&quot;, &quot;build&quot;, &quot;dist&quot;, &quot;jest&quot;]
}

答案1

得分: 1

今天遇到了同样的问题,不太确定为什么 Vercel 不喜欢某些字体,但尝试从不同的地方下载字体文件。您正在使用的路径并不是问题所在。https://github.com/orgs/vercel/discussions/2367

英文:

Ran into the same issue today, not really sure why vercel doesn't like some fonts but try downloading the font file from a different place. the path you're using is not the problem. https://github.com/orgs/vercel/discussions/2367

huangapple
  • 本文由 发表于 2023年5月25日 08:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328245.html
匿名

发表评论

匿名网友

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

确定