我正在React中构建一个JSON编辑器,如果我想创建目录列表,看起来Node fs模块是必经之路。
我觉得很傻,但是我不知道如何在应用程序中安装fs。我是否需要在应用程序内部安装整个Node.js才能使用“ fs”模块?是否有一个独立的“ fs”库执行相同的操作?
Here's the code I want to use. I tried yarn add fs
and was not surprised that I got rs.readdir is not a function
.
const testFolder = "/";
const fs = require("fs");
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
});
React runs in the browser. The Node.js
fs
module cannot run in the browser.The browser does not provide access to the user's file system (except in very limited ways, such as through
<input type="file">
). You cannot present a directory listing of the system the browser is running on from a webpage.如果要显示服务器文件系统的目录列表,请编写一个Web服务并通过Ajax与之交互。
(或者,如果需要独立的应用程序而不是Web应用程序,请使用类似Electron的东西)。