【问题标题】:Next.js: exportPathMap not found despite having one in next.config.jsNext.js:尽管在 next.config.js 中有一个 exportPathMap,但未找到
【发布时间】:2019-12-19 12:13:53
【问题描述】:

错误:

No "exportPathMap" found in "next.config.js". Generating map from "./pages"


但我确实有基于official docsexportPathMap

我的next.config.js 包含:

const withCss = require("@zeit/next-css");
const withSass = require("@zeit/next-sass");
const withTM = require("next-transpile-modules");

module.exports = {
  exportPathMap: async function(
    defaultPathMap,
    { dev, dir, outDir, distDir, buildId }
  ) {
    return {
      "/": { page: "/" },
      "/menu": { page: "/menu" },
      "/about": { page: "/about" }
    };
  }
};

module.exports = withCss({
  cssModules: true
});

module.exports = withSass(
  withTM({
    transpileModules: ["react-bulma-components"],
    sassLoaderOptions: {
      includePaths: ["./components"]
    }
  })
);

我也尝试过删除默认映射:

module.exports = {
  exportPathMap: async function() {
    return {
      "/": { page: "/" },
      "/menu": { page: "/menu" },
      "/about": { page: "/about" }
    };
  }
};

根据我的研究,除了将其移动到withCss() 中之外:

module.exports = withCss({
  exportPathMap: async function() {
    return {
      "/": { page: "/" },
      "/menu": { page: "/menu" },
      "/about": { page: "/about" }
    };
  }
});

withSass()withCss() 的两个导出似乎可以正常工作, 我做错了什么?


编辑:

我的next.config.js 位于项目根目录中,如果您想知道的话。

【问题讨论】:

    标签: javascript next.js


    【解决方案1】:

    您正在多次重新分配您的 module.exports,因此 exportPathMapwithCss 丢失了。在这种情况下,配置应如下所示:

    module.exports = withCss(
      withSass(
        withTM({
          transpileModules: ["react-bulma-components"],
          sassLoaderOptions: {
            includePaths: ["./components"]
          },
          exportPathMap: async function(
            defaultPathMap,
            { dev, dir, outDir, distDir, buildId }
          ) {
            return {
              "/": { page: "/" },
              "/menu": { page: "/menu" },
              "/about": { page: "/about" }
            };
          }
        })
      )
    );
    

    【讨论】:

    • 非常感谢。我只是这种出口的新手。还想指出您有一些语法错误:statement expectedunexpected token (eslint)
    【解决方案2】:

    next.js 贡献者目前不鼓励 exportPathMap。请看https://github.com/zeit/next.js/issues/10983#issuecomment-611147932

    否则正确答案是

    module.exports = withCss(
      withSass(
        withTM({
          transpileModules: ["react-bulma-components"],
          sassLoaderOptions: {
            includePaths: ["./components"]
          },
          exportPathMap: async function(
            defaultPathMap,
            { dev, dir, outDir, distDir, buildId }
          ) {
            return {
              "/": { page: "/" },
              "/menu": { page: "/menu" },
              "/about": { page: "/about" }
            };
          }
        })
      )
    );
    

    【讨论】:

    • info - Using webpack 5. Reason: Enabled by default https://nextjs.org/docs/messages/webpack5 info - using build directory: /app/.next info - Copying "static build" directory info - No "exportPathMap" found in "next.config.js". Generating map from "./pages" 我从构建的默认行为中得到这些 - 信息日志暗示需要一个 - 即,如果接下来要删除它,默认行为可能会引起一些混乱。
    【解决方案3】:
    module.exports = withCss(withSass(
     withTM({
     transpileModules: ["react-bulma-components"],
    sassLoaderOptions: {
      includePaths: ["./components"]
    },
    cssModules: true,
    exportPathMap: async function(
        defaultPathMap,
        { dev, dir, outDir, distDir, buildId }
    ) {
        return {
        "/": { page: "/" },
        "/menu": { page: "/menu" },
        "/about": { page: "/about" }
        };
    }
    })
    ));
    

    【讨论】:

    • 本网站上通常不赞成仅使用代码的答案。您能否编辑您的答案以包含一些 cmets 或对您的代码的解释?解释应回答以下问题:它有什么作用?它是如何做到的?它去哪儿了?它如何解决OP的问题?见:How to anwser。谢谢!
    猜你喜欢
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 2019-05-06
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多