"next/font" requires SWC although Babel is being used due to a custom babel config being present
Asked Answered
I

2

11

Im facing this error when I try to run my Nextjs project using styled components.

This is my .babelrc

{
  "plugins": [
    [
      "babel-plugin-styled-components",
      {
        "ssr": true,
        "displayName": true
      }
    ]
  ],
  "presets": ["next/babel", "@babel/preset-typescript"]
}

this is my next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  styledComponents:
    Boolean |
    {
      displayName: Boolean | undefined,
      ssr: Boolean | undefined
    }
}
module.exports = nextConfig

then this is my error: Syntax error: "next/font" requires SWC although Babel is being used due to a custom babel config being present. Read more: https://nextjs.org/docs/messages/babel-font-loader-conflict

I tried using custom babel config and in the next.config.js but not work as expected.

And, this is my _Document:

import Document, {
  Html,
  Head,
  Main,
  NextScript,
  DocumentContext
} from 'next/document'
import { ServerStyleSheet } from 'styled-components'

export default class MyDocument extends Document {
  static async getInitialProps(ctx: DocumentContext) {
    const sheet = new ServerStyleSheet()
    const originalRenderPage = ctx.renderPage

    try {
      ctx.renderPage = () =>
        originalRenderPage({
          enhanceApp: (App) => (props) =>
            sheet.collectStyles(<App {...props} />)
        })

      const initialProps = await Document.getInitialProps(ctx)
      return {
        ...initialProps,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        )
      }
    } finally {
      sheet.seal()
    }
  }

  render() {
    return (
      <Html lang="pt-BR">
        <Head />
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}
Ingaborg answered 7/4, 2023 at 15:1 Comment(1)
That error is because you're importing a font from the next/font module. It usually does it in the_app.tsx file by default. To import fonts, use an import statement inside your globals.cssJezebel
B
-1

The "Babel and next/font Conflict" error occurs when we use next/font with a custom Babel config. When we have a custom Bible config we opt out of the Next.js compiler which is required when using next/font

For this reason to resolve this error we can remove the Babel config for example the .babelrc.

This page is a good reference for more details on the error: https://nextjs.org/docs/messages/babel-font-loader-conflict

Begun answered 29/11, 2023 at 10:18 Comment(1)
"custom Bable config", check the "Bible" wordNoise
E
-4

Delete in .babelrc file this code
"presets": ["next/babel", "@babel/preset-typescript"] and it is will be working

Elastomer answered 10/4, 2023 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.