I'm trying to write a electron app with React.js and this app accesses file system.
Here is my fileWalker.js:
const path = require('path');
const fs = require('fs');
function *walkFolders (folder, recurseLevel = 0) {
try {
const files = fs.readdirSync(folder);
for (const file of files) {
try {
const pathToFile = path.join(folder, file);
const stat = fs.statSync(pathToFile);
const isDirectory = stat.isDirectory();
if (isDirectory && recurseLevel > 0) {
yield * walkFolders(pathToFile, recurseLevel - 1)
}
else {
yield {
rootDir: folder,
fileName: file,
isDir: isDirectory,
stat: stat
}
}
}
catch (err) {
yield {
rootDir: folder,
fileName: file,
error: err
}
}
}
}
catch (err) {
yield {
rootDir: folder,
error: err
}
}
}
export default walkFolders;
Then in my App.js, I wrote:
getFolders(absolutePath) {
let folders = [];
// check incoming arg
if (!absolutePath || typeof absolutePath !== 'string') {
return folders
}
for (const fileInfo of walkFolders(absolutePath, false)) {
// all files and folders
console.log(fileInfo);
if ('error' in fileInfo) {
console.error(`Error: ${fileInfo.rootDir} - ${fileInfo.error}`);
continue
}
// we only want folders
if (!fileInfo.isDir) {
continue
}
const node = this.createNode(fileInfo)
folders.push(node)
}
return folders
}
...
componentDidMount() {
this.getFolders("E:/gits");
}
constructor(props){
super(props);
this.getFolders=this.getFolders.bind(this);
...
}
...
But I get this error:
TypeError: fs.readdirSync is not a function
How can I resolve this?
