使用react和next.js时找不到fsmodule的错误如何解决

IT技术 node.js reactjs next.js
2021-05-13 18:21:14

我正在使用没有路由器设置的react应用程序。我想构建我的 sitemap.xml 文件。我尝试了一些module,如 sitemap.js、react-router-sitemap、sitemap-generator。但是这些module由于缺少 fs module而引发错误。我通过npm install --save. 但它仍然显示错误。

我在一些论坛中发现在 webpack.config 文件中添加以下代码。

node: { fs: "empty" } 我不确定这个文件在哪里。我在站点地图相关module中找不到它们。

请帮我解决这个问题。我是新来的react。

这是我的文件夹结构。 在此处输入图片说明

1个回答

创建 next.config.js 并放置以下代码。这对我来说可以。

下一个.config.js

module.exports = {
    webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
      // Note: we provide webpack above so you should not `require` it
      // Perform customizations to webpack config
      // Important: return the modified config

      // Example using webpack option
      //config.plugins.push(new webpack.IgnorePlugin(/\/__tests__\//))
      config.node = {fs:"empty"}
      return config
    },
    webpackDevMiddleware: config => {
      // Perform customizations to webpack dev middleware config
      // Important: return the modified config
      return config
    },
  }