Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine NPM modules used from a running node.js application

Tags:

node.js

npm

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?

like image 742
MrEvil Avatar asked Apr 22 '12 06:04

MrEvil


People also ask

How do I check Node.js modules?

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.

How do I see all npm modules?

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 .

How do I know what version of npm my project is using?

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].

How can you see all the dependencies for a Node.js app?

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.


1 Answers

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
like image 164
Pero P. Avatar answered Sep 19 '22 02:09

Pero P.