简体   繁体   English

NUXT:找不到模块:错误:无法解析“fs”

[英]NUXT: Module not found: Error: Can't resolve 'fs'

I'm starting out with vue and nuxt, I have a project using vuetify and I'm trying to modify the carousel component to dynamically load images from the static folder.我从 vue 和 nuxt 开始,我有一个使用 vuetify 的项目,我正在尝试修改轮播组件以从 static 文件夹动态加载图像。 So far I've come up with:到目前为止,我想出了:

    <template>
    <v-carousel>
        <v-carousel-item v-for="(item,i) in items" :key="i" :src="item.src"></v-carousel-item>
    </v-carousel>
    </template>


    <script>
    function getImagePaths() {
        var glob = require("glob");
        var options = {
        cwd: "./static"
        };
        var fileNames = glob.sync("*", options);
        var items = [];
        fileNames.forEach(fileName =>
        items.push({
            'src': '/'+fileName
        })
        );
        return items;
    }
    export default {
        data() {
        return {items :getImagePaths()};
        }
    };

    </script>

When I test this I see:当我测试这个时,我看到:

ERROR in ./node_modules/fs.realpath/index.js
Module not found: Error: Can't resolve 'fs' in '....\node_modules\fs.realpath'
ERROR in ./node_modules/fs.realpath/old.js
Module not found: Error: Can't resolve 'fs' in ....\node_modules\fs.realpath'
ERROR in ./node_modules/glob/glob.js
Module not found: Error: Can't resolve 'fs' in '....\node_modules\glob'
ERROR in ./node_modules/glob/sync.js
Module not found: Error: Can't resolve 'fs' in '.....\node_modules\glob'

googling this I see a bunch of references like https://github.com/webpack-contrib/css-loader/issues/447 .谷歌搜索这个我看到一堆参考像https://github.com/webpack-contrib/css-loader/issues/447

These suggest that you have to midify the webpack config file with something like:这些建议您必须对 webpack 配置文件进行如下修改:

node: {
  fs: 'empty'
}

I know very little about webpack.我对 webpack 知之甚少。 I found https://nuxtjs.org/faq/extend-webpack/ , but am not sure how to modify the webpack config file in this case.我找到了 https://nuxtjs.org/faq/extend-webpack/ ,但不确定在这种情况下如何修改 webpack 配置文件。

How do I do this?我该怎么做呢?

You can't use NodeJs specific module on browser. 您不能在浏览器上使用NodeJ的特定模块。

To solve your issue, you can create an API using Nuxt server middleware. 为了解决您的问题,您可以使用Nuxt服务器中间件创建API。 The code below, inspired by https://github.com/nuxt-community/express-template . 以下代码受https://github.com/nuxt-community/express-template的启发。

  1. Create a file, index.js in api/index.js . 创建一个文件, index.jsapi/index.js Then fill it with: 然后填充:

     const express = require('express') // Create express instance const app = express() // Require API routes const carousel = require('./routes/carousel') // Import API Routes app.use(carousel) // Export the server middleware module.exports = { path: '/api', handler: app } 
  2. Create carousel.js in api/routes/carousel.js . 创建carousel.jsapi/routes/carousel.js Then fill it with: 然后填充:

     const { Router } = require('express') const glob = require('glob') const router = Router() router.get('/carousel/images', async function (req, res) { const options = { cwd: './static' } const filenames = glob.sync('*', options) let items = []; filenames.forEach(filename => items.push({ 'src': '/'+filename }) ); return res.json({ data: items }) }) module.exports = router 
  3. Register your server middleware in nuxt.config.js nuxt.config.js注册服务器中间件

     module.exports = { build: { ... }, serverMiddleware: [ '~/api/index.js' ] } 
  4. Call the api in your page / component. 在页面/组件中调用api。 I assume you're using Axios here ( axios-module ). 我假设您在这里使用Axios( axios-module )。

     <script> export default { async asyncData ({ $axios }) { const images = (await $axios.$get('/api/carousel/images')).data return { images } } } </script> 

I know this is an old question, but it may be helpful for someone to disable fs in their browser.我知道这是一个老问题,但对于在浏览器中禁用 fs 的人来说可能会有所帮助。
Like this:像这样:

nuxt.config.js nuxt.config.js

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

       config.node= {
          fs: 'empty'
        }

       // ....
    }
  },

add this in your nuxt-config.js build: { extend (config, { isDev, isClient }) {将此添加到您的 nuxt-config.js 构建中:{ extend (config, { isDev, isClient }) {

config.node = {
     fs: 'empty'
 }

// ....

}}, }},

暂无
暂无

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

相关问题 如何解决 nuxt.js 中的“找不到模块:错误:无法解析‘fs’”? - How to resolve the "Module not found: Error: Can't resolve 'fs'" in nuxt.js? 未找到模块:错误:无法解析“加密”和无法解析“fs” - Module not found: Error: Can't resolve 'crypto' and Cant resolve 'fs' 找不到模块:错误:无法使用 webpack 解析“fs” - Module not found: Error: Can't resolve 'fs' with webpack 找不到模块:错误:无法使用 Webpack 解析“fs” - Module not found: Error: Can't resolve 'fs' Using Webpack Webpack-NodeJS-找不到模块:错误:无法解析“ fs” - Webpack - NodeJS - Module not found: Error: Can't resolve 'fs' 找不到模块:无法解析 Next.js 应用程序中的“fs” - Module not found: Can't resolve 'fs' in Next.js application 找不到模块:无法解析 @google-cloud/storage 上的“fs” - Module not found: Can't resolve 'fs' on @google-cloud/storage 找不到模块:错误:尝试使用 NodeJS 库时无法解析“fs” - Module not found: Error: Can't resolve 'fs' when trying to use a NodeJS library 对于 next.js,为什么我突然开始看到找不到模块:错误:无法解析“fs”? - With next.js, why did I suddenly start seeing Module not found: Error: Can't resolve 'fs'? 找不到模块:错误:无法解析“/node_modules/dotenv/lib”中的“fs”-创建反应应用程序 - Module not found: Error: Can't resolve 'fs' in '/node_modules/dotenv/lib' - create react app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM