Other than grabbing the package.json
file at the project root is there a way to determine the list of dependencies of a running node.js application? Does node keep this meta information available as some var in the global namespace?
The core modules are defined within the Node.js source and are located in the lib/ folder. Core modules can be identified using the node: prefix, in which case it bypasses the require cache.
Use the npm list to show the installed packages in the current project as a dependency tree. Use npm list --depth=n to show the dependency tree with a specified depth. Use npm list --prod to show packages in the dependencies . Use npm list --dev to show packages in the devDependencies .
To discover npm version checks are currently installed in your project, run npm list. All the npm modules available are: the latest version of [email protected]. This [email protected].
In every Node application there should be a file in the root folder of the application called package. json. This file can contain various metadata about a project, including the packages that it depends on to run.
If you are just looking for the currently installed npm packages in the application directory, then you can install the npm package (npm install -g npm
) and programatically invoke ls
to list the installed packages and the dependency trees.
Obviously, this has no bearing on whether the installed packages are actually require
'd in the application or not.
Usage is not that well documented but this should get you started.
var npm = require('npm');
npm.load(function(err, npm) {
npm.commands.ls([], true, function(err, data, lite) {
console.log(data); //or lite for simplified output
});
});
e.g.:
{ dependencies:
{ npm: { version: '1.1.18', dependencies: [Object] },
request: { version: '2.9.202' } } }
Otherwise, I believe the only other option is to introspect the module
module to get information pertaining to the currently loaded/cached module paths. However this definitely does not look to have been developed as a public API. I'm not sure if there are any alternatives so would be keen to hear if there are e.g.
var req = require('request'); // require some module for demo purposes
var m = require('module');
// properties of m contain current loaded module info, e.g. m._cache
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With