使用react和next.js时找不到fs模块的错误怎么解决 [英] how to solve the error that fs module is not found when used react and next.js

查看:49
本文介绍了使用react和next.js时找不到fs模块的错误怎么解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Am using a react application without router settings. I want to build my sitemap.xml file. I tried some modules like sitemap.js, react-router-sitemap, sitemap-generator. But these module are throwing error as fs module is missing. I installed fs module via npm install --save. But it is still showing the error.

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

I found in some forums to add the below code in webpack.config file.

节点:{fs:空"}我不确定这个文件在哪里.我在站点地图相关模块中找不到它们.

node: { fs: "empty" } Am not sure where this file is. I couldn't find them nside the sitemap related modules.

请帮我解决这个问题.刚开始反应.

Please help me to resolve this. Am new to react.

这是我的文件夹结构.

推荐答案

创建 next.config.js 并放入下面的代码.对我来说效果很好.

create next.config.js and put below code. It works fine for me.

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
    },
  }

这篇关于使用react和next.js时找不到fs模块的错误怎么解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆