I am trying to get some of the NPM functionality into my Node.js programs. In particular, I would like to be able to analyze the available node modules on my system. "Module" here means "module identifier", thus either an identifier like "fd", or a file path; in other words, anything that can be put into a require()
call and does load a module.
This question is split into three sub-problems:
1) Get a list of all core modules
2) Get a list of all loaded modules
3) Get a list of all installed and available modules.
Question 1 is answered by zeke's node-core-module-names list. Loading another module to find the core modules list is not elegant and it may outdate itself, but it is an option and does work. The (ordered) list is thus ['assert', 'buffer', 'child_process', ..., 'zlib']
.
The 2. question can be answered by a call of Object.keys(require.cache)
, which returns a list of file paths.
What I cannot elegantly solve by now is the 3. question. There is the shell command npm ls
which returns a tree graph. But is there anything usable and better?
Thank's for listening! Tom