mihneadb / node-directory-tree

Convert a directory tree to a JS object.
MIT License
523 stars 106 forks source link

try { stats = FS.statSync(path); } entering exception and returning null #48

Closed DanielVarzim closed 6 years ago

DanielVarzim commented 6 years ago

Hi, I am trying to use the directory-tree module, but the function is returning null. What am I doing wrong here? Do I need specific modules installed?

Code example:

import React, { Component } from 'react'; import directoryTree from 'directory-tree'; import _ from 'lodash';

let tree = directoryTree("C:/Users/Daniel Varzim/Desktop/Apps");

class Dummy extends Component { render() { console.log(tree) return (

Welcome to the KnowledgeBase web platform
            {this.renderTree(tree)}
        </div>
    )
}

renderTree(tree) {
    return _.map(tree, app => {
        return (
            <tr>
                <th>
                    {app.name}
                </th>
            </tr>
        )
    });
}

} export default Dummy;

DanielVarzim commented 6 years ago

Why does "Stats" comes undefined? Is "C:/Users/Daniel Varzim/Desktop/Apps" not being considered a directory?

DanielVarzim commented 6 years ago

This is the exception I get:

"e: TypeError: FS.statSync is not a function at directoryTree"

I have the fs, fs-extra and graceful-fs modules.

mihneadb commented 6 years ago

Hi, your issue probably is the fact that you are trying to call this from a browser where you don't have fs available. You would need a node environment.

DanielVarzim commented 6 years ago

With the project I am developing I just need a front-end for a folder containing some files. I don´t know if I understood your answer, do I need some sort of back-end node server to run your module?

I am using react and redux, and I just want to have a const with the folder structure in json so I can display the files in the folder.

mihneadb commented 6 years ago

Yes, you need a server with access to the filesystem. You can simply run an API server that serves the JSON of the folder on some endpoint.