Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
WonderboyPark opened this issue Aug 16, 2021 · 6 comments
Open

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

WonderboyPark opened this issue Aug 16, 2021 · 6 comments

Comments

@WonderboyPark
Copy link

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
Copy link
Owner

mihneadb commented Aug 16, 2021

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

@WonderboyPark
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

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

  • in your current directory (json), make a new folder with a file in it
  • run dirTree(".")

See if that works.

@WonderboyPark
Copy link
Author

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
Copy link
Owner

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:

  • run node
  • run the same commands but in the shell, not in your file
  • console.log(tree.... tab tab play with it)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants