Reputation: 7153
I'm building a JSON editor in React and it looks like the Node fs module is the way to go if I want to create a directory listing.
I feel dumb but I don't know how to install fs in the app. Do I need to install the entire Node.js inside the app to use the 'fs' module? Is there a stand-alone "fs" library that does the same thing?
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);
});
});
Upvotes: 1
Views: 1925
Reputation: 943556
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.
If you want to present a directory listing of the server's file system, then write a web service and interact with it via Ajax.
(Or use something like Electron if you want a stand-alone application and not a web app).
Upvotes: 2