【发布时间】:2021-07-06 19:10:43
【问题描述】:
我正在尝试从文件夹“数据”中读取文件,下面的代码应该可以正常工作,但我无法理解为什么会出现此错误:
Uncaught TypeError: _fs.readdir is not a function
import fs from "fs";
// Have tried the below option too instead of import sytax
// const fs = require("fs");
const dataFolder = "./data/";
// Option #1
fs.readdir(dataFolder, function (_err, files) {
files.forEach((file) => console.log(file));
});
// OUTPUT
// Uncaught TypeError: _fs.readdir is not a function
// Option #2
fs.readdirSync(dataFolder).forEach((file) => console.log(file));
// OUTPUT
// Uncaught TypeError: _fs.readdirSync is not a function
任何帮助将不胜感激。我以为是捆绑程序 Parcel 导致的,但我将其更改为 Webpack,它仍然给出相同的错误。
【问题讨论】:
-
你能提供完整的错误回溯吗?你能提供你的 webpack 配置吗?
-
@SuperStormer 我目前正在使用 parcel 进行捆绑。我在测试时使用了基本的 webpack 配置。直接来自教程,它给出了与 parcel 相同的错误,所以我想回到包裹..关于错误回溯..你能帮我看看我怎么能帮你得到这个吗?我对此很陌生..
-
@SuperStormer : 我已经在 netlify 上上传了这个网站。你可以在控制台中看到错误。希望对你有帮助。brahma-gallery.netlify.app
-
fs是一个 node.js 模块,仅在服务器端可用。 -
@ĐăngKhoaĐinh : 哦,好吧..现在有道理了..现在将从这个角度进行测试和检查..感谢分享..为麻烦道歉..
标签: javascript fs