简体   繁体   English

如何解决 nuxt.js 中的“找不到模块:错误:无法解析‘fs’”?

[英]How to resolve the "Module not found: Error: Can't resolve 'fs'" in nuxt.js?

I'm a bit new to node, but from a lot of searching, it looks like 'fs' broke a lot of things in the past.我对节点有点陌生,但从大量搜索来看,过去似乎“fs”破坏了很多东西。 I've come across several packages I've tried to install via npm and have run into the Module not found: Error: Can't resolve 'fs' error way too much.我遇到了几个尝试通过 npm 安装的软件包,但遇到了Module not found: Error: Can't resolve 'fs'错误太多。

I've run the npm install, and fs downloads a placeholder package , but I'm really at a halt because a package (among several) still has a dependency on fs.我已经运行了 npm install,并且 fs 下载了一个占位符包,但我真的处于停顿状态,因为一个包(在几个中)仍然依赖于 fs。

Nearly every solution I find has resulted in declaring fs as empty in the node section of the webpack settings:发现的几乎所有解决方案都导致在 webpack 设置的节点部分将fs声明为空:

node: {
  fs: 'empty'
},

Unfortunately, I'm using Vue.js and nuxt and there is no webpack settings file (that I know of).不幸的是,我正在使用 Vue.js 和 nuxt 并且没有 webpack 设置文件(我知道)。 I've tried to add it into my nuxt_config.js but haven't been successful.我试图将它添加到我的 nuxt_config.js 中,但没有成功。 extend(config, ctx) { config.node = { fs: "empty" };扩展(配置,ctx){ config.node = { fs:“空”}; } }

Is there a way to run the exclude inside of the nuxt_config?有没有办法在 nuxt_config 中运行 exclude? Also, is there a way to run it while still preserving my settings to run eslint on save?另外,有没有办法在运行它的同时仍然保留我的设置以在保存时运行 eslint?

extend(config, ctx) {
  // Run ESLint on save
  if (ctx.isDev && ctx.isClient) {
    config.module.rules.push({
      enforce: 'pre',
      test: /\.(js|vue)$/,
      loader: 'eslint-loader',
      exclude: /(node_modules)/
    })
  }
}

Thanks.谢谢。

The usage may change according to versions.用法可能会因版本而异。

In my case, i've to define confing.node with "=" character.就我而言,我必须用“=”字符定义 confing.node。

    build: {
        extend (config, { isDev, isClient }) { 
            config.node = {
                fs: "empty"
            }
        })
    }

The suggestion is correct, according to the webpack see the documentation you should set the fs module to 'empty' .该建议是正确的,根据 webpack 请参阅文档,您应该将fs模块设置为'empty'

Have you tried inserting it into your nuxt configuration on the top level config within build block?您是否尝试将其插入到构建块中顶级配置的 nuxt 配置中?

build: {
  extend (config, { isDev, isClient }) {

     config.node: {
        fs: 'empty'
      }

     // ....
  }
}

Just in case it helps.以防万一它有帮助。 In my case this error appeared all of a sudden and wasn't able to tell why.在我的情况下,这个错误突然出现并且无法说明原因。 After trying everything (deleting and reinstalling npm packages, etc...) I found out that VS CODE auto referenced a package in a file I had just saved and I didn't notice.在尝试了一切(删除和重新安装 npm 包等...)之后,我发现 VS CODE 自动引用了我刚刚保存的文件中的一个包,我没有注意到。

In my case it added import { query } from 'express'.就我而言,它添加了import { query } from 'express'.

Deleted the line and everything worked again.删除了该行,一切又恢复了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NUXT:找不到模块:错误:无法解析“fs” - NUXT: Module not found: Error: Can't resolve 'fs' 找不到模块:错误:无法解析“fs”反应 js - Module not found: Error: Can't resolve 'fs' react js 未找到模块:错误:无法解析“加密”和无法解析“fs” - Module not found: Error: Can't resolve 'crypto' and Cant resolve 'fs' 找不到模块:无法解析 Next.js 应用程序中的“fs” - Module not found: Can't resolve 'fs' in Next.js application Webpack-NodeJS-找不到模块:错误:无法解析“ fs” - Webpack - NodeJS - Module not found: Error: Can't resolve 'fs' 找不到模块:错误:无法使用 Webpack 解析“fs” - Module not found: Error: Can't resolve 'fs' Using Webpack 找不到模块:错误:无法使用 webpack 解析“fs” - Module not found: Error: Can't resolve 'fs' with webpack 找不到模块:错误:无法解析 dotenv/lib 中的“fs” - Module not found: Error: Can't resolve 'fs' in dotenv/lib 对于 next.js,为什么我突然开始看到找不到模块:错误:无法解析“fs”? - With next.js, why did I suddenly start seeing Module not found: Error: Can't resolve 'fs'? React - 未找到模块:错误:无法在 .. 中解析 'fs' 并且无法在中解析 .net' - React - Module not found: Error: Can't resolve 'fs' in.. and can't resolve 'net' in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM