未找到模块:无法在 Next.js 应用程序中解析“fs" [英] Module not found: Can't resolve 'fs' in Next.js application

查看:100
本文介绍了未找到模块:无法在 Next.js 应用程序中解析“fs"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法确定我的 next.js 应用中发生了什么.因为 fs 是 nodejs 的默认文件系统模块.它给出了module not found的错误.

Unable to identify what's happening in my next.js app. As fs is a default file system module of nodejs. It is giving the error of module not found.

推荐答案

如果你使用 fs,请确保它只在 getInitialPropsserverSideProps.(包括服务器端渲染).

If you use fs, be sure it's only within getInitialProps or serverSideProps. (anything includes server-side rendering).

您可能还需要创建一个包含以下内容的 next.config.js 文件来构建客户端包:

You may also need to create a next.config.js file with the following content to get the client bundle to build:

对于 webpack4

module.exports = {
  webpack: (config, { isServer }) => {
    // Fixes npm packages that depend on `fs` module
    if (!isServer) {
      config.node = {
        fs: 'empty'
      }
    }

    return config
  }
}

对于webpack5

module.exports = {
  webpack5: true,
  webpack: (config) => {
    config.resolve.fallback = { fs: false };

    return config;
  },
};

注意:对于其他模块如path,可以添加多个参数如

Note: for other modules such as path, you can add multiple arguments such as

{
  fs: false,
  path: false
}

这篇关于未找到模块:无法在 Next.js 应用程序中解析“fs"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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