I want to introduce the dependencies I have in a d3.js tree and I was wondering if there is any way I can list the dependencies name dynamically in a json file or into the javascript file itself. I don't understand how javascript can read into folder names without external help. I do not wish to use any outside dependencies or server-side javascript.
Asked
Active
Viewed 167 times
0
-
1It's hard to answer that without any code or any hint about your dependencies structure :/ – Jeremy Thille Sep 05 '17 at 15:03
-
@JeremyThille ... src/script/myjsfile.js ... node_modules/ //the folder names I want to read ... package.json – Ruxandra Anghel Sep 05 '17 at 15:22
-
I'm willing to help, but you didn't give any information about your code or your dependencies or whatever. So it's a bit hard. `javascript can read into folder names` no, it can't. Javascript doesn't have access to the file system, so again, it's hard to answer you without any info about what you're trying to do. – Jeremy Thille Sep 05 '17 at 15:24
-
You don't want to use any server-side code to read what's in `node_modules` ? :) But NodeJS is pure server-side code... Not sure what's going on here – Jeremy Thille Sep 05 '17 at 15:26
-
@JeremyThille I'm trying to get the node modules folder names and put them into an array. Apologies, I can't share the code. – Ruxandra Anghel Sep 05 '17 at 15:26
-
Without any server-side code?? Well you just can't do that O_o Client-side javascript has no access to the file system for obvious security reasons. And anyway, the `node_modules` folder is on your server, not on your client. – Jeremy Thille Sep 05 '17 at 15:27
-
How would you do it with server side code? – Ruxandra Anghel Sep 05 '17 at 15:28
-
simply with the `fs` (File System) module. `fs.readDir("./node_modules")` or something, check out the doc – Jeremy Thille Sep 05 '17 at 15:29
-
but why would you need that anyway? It's precisely `package.json`'s job to hold a list of all your dependencies in one place. – Jeremy Thille Sep 05 '17 at 15:32
-
I want to make a visual representation with a d3 tree. – Ruxandra Anghel Sep 05 '17 at 15:37
-
Any luck? I found [this](https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search), it might help you. But anyway, forget scanning a directory with client-side JS, it's just not allowed to do that. – Jeremy Thille Sep 06 '17 at 07:18
1 Answers
0
In your package.json you have all the dependencies of your project and each dependency has its own package.json at the end there is a tree of dependencies that is built,
you can access the package.json with a simple
var package = require ('./path/package.json');
console.log(package.devDependencies);
console.log(package.dependencies);
i hope i have answered your question :)

Hajji Tarik
- 1,072
- 7
- 23
-
2@RuxandraAnghel When you get an error message, then don't paraphrase it but copy it as is. Getting the error "Package is undefined" from the code above is simply not possible. – str Sep 05 '17 at 16:09