mihneadb / node-directory-tree

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

How do I run this?(Node/NPM beginner) #92

Open WonderboyPark opened 3 years ago

WonderboyPark commented 3 years ago

Please forgive me for this question, but I literally just started looking into node.js and NPM the directory tree > JSON is exactly what i need for a project at work. But how do I run this?

I installed Node.js and installed directory-tree, have a app.js and can see stuff in the terminal(console.log("Hello")).

Do i just copy the following into app.js and run it after i change the dirTree path?

const dirTree = require("directory-tree"); const tree = dirTree("/some/path");

When i have tried this the response is [Function: directoryTree]. will the JSON show up in the terminal?

again sorry about knowing nothing really but any help would be much appreciated

mihneadb commented 3 years ago

Hi! Did you try logging tree? console.log(JSON.stringify(tree));

WonderboyPark commented 3 years ago

i get a null response when i try console.log(JSON.stringify(tree));

right now this is what i have in the app.js file const dirTree = require("directory-tree"); const tree = dirTree("C:\Users\steve\Desktop"); console.log(JSON.stringify(tree));

WonderboyPark commented 3 years ago

this is what i am getting PS C:\Users\steve\Desktop\json> node -v v14.17.5 PS C:\Users\steve\Desktop\json> npm -v 7.20.6 PS C:\Users\steve\Desktop\json> node app.js null

mihneadb commented 3 years ago

The lib also returns null if it does not have permissions to access a path. Try the following:

See if that works.

WonderboyPark commented 3 years ago

ok this worked so that I get JSON in the terminal, but types that are directory are only showing the children as an array... but not actually showing the objects in that array. this is really close to being what i need

extension: '.ai', type: 'file' }, { path: 'Business Card', name: 'Business Card', children: [Array], size: 53635122, type: 'directory' }, {

How can i get the children as an array of objects? Im getting this as a console.log, but you mentioned i need to "run" dirTree.... im not sure what mean by "run". what is the script i should be using to run something like this and is it my app.js, or in the terminal?(really sorry about the noob questions, but if i try and figure this out on my own any longer, the computer is gonna learn how to fly)

mihneadb commented 3 years ago

That's what I meant by running, what you did. It worked, the reason you see [Array] instead of the actual array is just representation. The data is there :). You can check by accessing the tree object. You can also play with it in the terminal: