[英]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的启发。
Create a file, index.js
in api/index.js
. 创建一个文件,
index.js
在api/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 }
Create carousel.js
in api/routes/carousel.js
. 创建
carousel.js
在api/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
Register your server middleware in nuxt.config.js
在
nuxt.config.js
注册服务器中间件
module.exports = { build: { ... }, serverMiddleware: [ '~/api/index.js' ] }
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.