Skip to content

Instantly share code, notes, and snippets.

@santthosh
Created August 22, 2014 21:30
Show Gist options
  • Save santthosh/d86e9f1e547c201b319d to your computer and use it in GitHub Desktop.
Save santthosh/d86e9f1e547c201b319d to your computer and use it in GitHub Desktop.
Get details on nodejs project dependencies from package.json
'use strict';
/*
* Generates a report of npm components the project uses for compliance
* Author: @santthosh
*/
var npm = require('npm');
var viewPackageInfo = function(err, data) {
for(var version in data) {
if(data.hasOwnProperty(version)) {
var info = data[version];
var repositoryUrl;
if(typeof info.repository.url !== 'undefined') {
repositoryUrl = info.repository.url;
} else {
repositoryUrl = 'undefined';
}
var author;
if(typeof info.author !== 'undefined') {
author = info.author.name;
} else {
author = 'undefined';
}
var licenseType, licenseUrl;
if(typeof info.licenses !== 'undefined') {
licenseType = info.licenses.type;
licenseUrl = info.licenses.url;
} else {
licenseType = 'undefined';
licenseUrl = 'undefined';
}
console.log(info.name +
',' + repositoryUrl +
',' + author +
',' + licenseType +
',' + licenseUrl);
}
}
};
npm.load(function (er, npm) {
npm.config.set('json',true);
npm.config.set('depth',0);
npm.commands.list([],true,function(err, data) {
var dependencies = [];
for(var key in data.dependencies) {
if(data.dependencies.hasOwnProperty(key)) {
npm.commands.view([key],true,viewPackageInfo);
}
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment