找不到模块:无法解析 Next.js 构建错误中的“../header/main-header.js”

问题描述 投票:0回答:1

layout.js 文件中,我收到错误

"Module not found: Can't resolve '../header/main-header.js'"

我的layout.js导入标头,例如

import MainHeader from "../header/main-header.js";

我知道文件名中有一个连字符,但它似乎对其他文件没有任何影响。在生产中一切正常,当我构建时仅此文件出现错误。

main.header.js 导出自身,例如

export default MainHeader;

我认为问题可能出在我下面的 next.config 中:

我不知道如何将

const nextConfig
与 webpack(config) 一起使用:

我还尝试使用不带

import MainHeader from "../header/main-header.js"
扩展名和其他全局路径的
.js
,但在构建时仍然无法工作。

/** @type {import('next').NextConfig} */
const nextConfig = {
  pageExtensions: ["ts", "tsx", "js", "jsx", "md", "mdx"],

  reactStrictMode: true,
};

module.exports = {
  webpack(config) {
    const fileLoaderRule = config.module.rules.find((rule) => rule.test?.test?.(".svg"));

    config.module.rules.push(
      {
        ...fileLoaderRule,
        test: /\.svg$/i,
        resourceQuery: /url/,
      },

      {
        test: /\.svg$/i,
        issuer: /\.[jt]sx?$/,
        resourceQuery: { not: /url/ },
        use: ["@svgr/webpack"],
      }
    );

    fileLoaderRule.exclude = /\.svg$/i;

    return config;
  },
};

以防万一我的

main-header.js
layout.js
文件:

import MainHeader from "../header/main-header.js";
import { Inter } from "@next/font/google";
import Footer from "../footer/footer";
import { useRouter } from "next/router";
import ErrorPage from "@/pages/404";
import { useState, useEffect } from "react";
import SquareLoader from "react-spinners/SquareLoader";

const inter = Inter({
  weight: ["300", "400", "500", "600", "700"],
  subsets: ["latin"],
});

const Layout = (props) => {
  const router = useRouter();
  const [pageLoading, setPageLoading] = useState(false);

  const storage = typeof window !== "undefined" ? window.localStorage.theme : "light";

  const override = {
    display: "block",
    margin: "0 auto",
    borderRadius: "3px",
    borderColor: "blue",
  };

  useEffect(() => {
    const handleStart = () => {
      setPageLoading(true);
    };
    const handleComplete = () => {
      setPageLoading(false);
    };

    router.events.on("routeChangeStart", handleStart);
    router.events.on("routeChangeComplete", handleComplete);
    router.events.on("routeChangeError", handleComplete);
  }, [router]);

  return router.pathname !== "/404" ? (
    <div
      className={`${inter.className} ${
        pageLoading ? "background__loading_theme" : "background-theme"
      }`}
    >
      <MainHeader />
      {pageLoading ? (
        <div className="loading-theme">
          <SquareLoader
            color={storage === "light" ? "#2b2b2b" : "#ffffff"}
            loading={pageLoading}
            cssOverride={override}
            size={70}
            aria-label="Loading Spinner"
            data-testid="loader"
          />
        </div>
      ) : (
        <main className="background">{props.children}</main>
      )}
      <Footer />
    </div>
  ) : (
    <div className={`${inter.className} error-theme`}>
      <ErrorPage />
    </div>
  );
};

export default Layout;


import Link from "next/link";
import { useState, useEffect } from "react";
import styles from "./main-header.module.css";
import menustyles from "./menu-icon.module.css";
import Image from "next/image";
import logo from "../../public/images/my-logo.png";
import logoDark from "../../public/images/my-logo-dark.png";
import SunIcon from "../../public/images/sun.svg";
import MoonIcon from "../../public/images/moon.svg";
import { useRouter } from "next/router";
import { useRef } from "react";
import MenuIcon from "./menu-icon";

const MainHeader = () => {
  const storage = typeof window !== "undefined" ? window.localStorage.theme : "light";
  const [themeMode, setThemeMode] = useState(storage);
  const [clientLoaded, setClientLoaded] = useState(false);

  const themeModeHandle = (e) => {
    e.preventDefault();
    setThemeMode((prev) => (prev === "dark" ? "light" : "dark"));
  };

  const navRef = useRef();
  const iconRef = useRef();
  const sunIconRef = useRef();

  const router = useRouter();

  const handleAnimation = () => {
    iconRef.current.classList.toggle(`${menustyles.openmenu}`);
  };

  const showNavbar = () => {
    navRef.current.classList.toggle(`${styles.responsive__nav}`);
    if (!sunIconRef.current.classList.contains(`${styles.responsive__icon}`)) {
      sunIconRef.current.classList.add(styles.responsive__icon);
    }
  };

  const closeNavbar = () => {
    navRef.current.classList.remove(`${styles.responsive__nav}`);
    if (iconRef.current.classList.contains(`${menustyles.openmenu}`)) {
      handleAnimation();
    }
    if (!sunIconRef.current.classList.contains(`${styles.responsive__icon}`)) {
      sunIconRef.current.classList.add(styles.responsive__icon);
    } else {
      sunIconRef.current.classList.remove(styles.responsive__icon);
    }
  };

  useEffect(() => {
    document.body.dataset.theme = themeMode;
    window.localStorage.setItem("theme", themeMode);
  }, [themeMode]);

  useEffect(() => {
    setClientLoaded(true);
  }, []);

  return (
    <header className={styles.header}>
      <div className={styles.header__main}>
        <div className={styles.header__menu}>
          <Link href="/">
            {(themeMode === "light" || themeMode === undefined) && clientLoaded ? (
              <Image
                src={logo}
                alt="website logo"
                className={styles.header__logo}
                priority={true}
              />
            ) : (
              <Image src={logoDark} alt="website logo" className={styles.header__logo} />
            )}
          </Link>
          <nav className={styles.navigation} ref={navRef}>
            <ul className={styles.navigation__ul}>
              <li
                onClick={closeNavbar}
                className={router.asPath === "/" ? `${styles.li__active}` : ""}
              >
                <Link href="/">Portfolio</Link>
              </li>
              <li
                onClick={closeNavbar}
                className={router.asPath === "/about" ? `${styles.li__active}` : ""}
              >
                <Link href="/about">About</Link>
              </li>
              <li
                onClick={closeNavbar}
                className={router.asPath === "/blog" ? `${styles.li__active}` : ""}
              >
                <Link href="/blog">Blog</Link>
              </li>
              <li
                onClick={closeNavbar}
                className={router.asPath === "/projects" ? `${styles.li__active}` : ""}
              >
                <Link href="/projects">Projects</Link>
              </li>
            </ul>
            <button className={styles.navigation__modalicons}>
              {themeMode === "light" && clientLoaded ? (
                <div ref={sunIconRef}>
                  <SunIcon
                    onClick={themeModeHandle}
                    className={`${styles.navigation__sun} ${styles.sun} `}
                  />
                </div>
              ) : (
                <div ref={sunIconRef}>
                  <MoonIcon
                    onClick={themeModeHandle}
                    className={`${styles.navigation__sun} ${styles.moon} `}
                  />
                </div>
              )}
            </button>
          </nav>
          <button
            className={styles.navigation__btn}
            onClick={showNavbar}
            aria-label="Button menu icon"
          >
            <MenuIcon animation={handleAnimation} ref={iconRef} />
          </button>
        </div>
        <button
          className={styles.navigation__icons}
          aria-label="Icon to change a theme of a website"
        >
          {themeMode === "light" && clientLoaded ? (
            <SunIcon
              onClick={themeModeHandle}
              className={`${styles.header__sun} ${styles.sun}`}
            />
          ) : (
            <MoonIcon
              onClick={themeModeHandle}
              className={`${styles.header__sun} ${styles.moon}`}
            />
          )}
        </button>
      </div>
    </header>
  );
};

export default MainHeader;
next.js vercel production
1个回答
0
投票

我发现问题是我在github上的一些文件夹是以大写字母命名的。我第一次上传项目的时候一定是用大写字母命名的,后来在本地改成了小写。由于 github 对更改不区分大小写,因此在我尝试投入生产之前,它们从未被采用。

确保检查存储库和本地生产中的文件名。

© www.soinside.com 2019 - 2024. All rights reserved.