NPM package analysis including local dependencies, global packages, version detection, and dependency tree traversal.
Analyze locally installed npm packages with support for glob patterns and dependency analysis.
/**
* Gets local npm package versions and dependency information
* @param packages - Package names array, glob pattern string, or boolean for all packages
* @param options - Collection options for duplicates, full tree traversal, etc.
* @returns Promise<[string, Object]> - ['npmPackages', packageInfoObject]
*/
function getnpmPackages(packages, options);Parameters:
packages:
string[] - Array of specific package names to checkstring - Glob pattern like "*webpack*" or "@babel/*"boolean - true to get all packages, false to skipoptions.duplicates - Show duplicate versions across dependency treeoptions.fullTree - Traverse entire node_modules tree, not just top-leveloptions.showNotFound - Include packages that aren't foundUsage Examples:
const { helpers } = require('envinfo');
const { getnpmPackages } = helpers;
// Get specific packages
const [name, packages] = await getnpmPackages(['react', 'react-dom', 'webpack']);
console.log(packages);
// Result: { react: '17.0.2', 'react-dom': '17.0.2', webpack: 'Not Found' }
// Get all packages matching a pattern
const [name, babelPackages] = await getnpmPackages('@babel/*');
console.log(babelPackages);
// Result: { '@babel/core': '7.17.5', '@babel/preset-env': '7.16.11' }
// Get all packages with duplicates
const [name, allPackages] = await getnpmPackages(true, {
duplicates: true,
fullTree: true
});
console.log(allPackages);
// Result: { lodash: { installed: '4.17.21', duplicates: ['4.17.20', '4.17.19'] } }
// Get packages from package.json dependencies only
const [name, topLevelPackages] = await getnpmPackages(true);
console.log(topLevelPackages);Analyze globally installed npm packages across the system.
/**
* Gets globally installed npm package versions
* @param packages - Package names array, glob pattern string, or boolean for all packages
* @param options - Collection options
* @returns Promise<[string, Object]> - ['npmGlobalPackages', globalPackageInfoObject]
*/
function getnpmGlobalPackages(packages, options);Usage Examples:
const { helpers } = require('envinfo');
const { getnpmGlobalPackages } = helpers;
// Get all global packages
const [name, globals] = await getnpmGlobalPackages(true);
console.log(globals);
// Result: { 'create-react-app': '5.0.0', 'npm': '8.5.0', 'yarn': '1.22.17' }
// Get specific global packages
const [name, specificGlobals] = await getnpmGlobalPackages([
'create-react-app',
'vue-cli',
'angular-cli'
]);
console.log(specificGlobals);
// Get global packages matching pattern
const [name, reactGlobals] = await getnpmGlobalPackages('*react*');
console.log(reactGlobals);// Package information structure for local packages
interface LocalPackageInfo {
[packageName: string]: string | {
installed?: string; // Version installed at top level
wanted?: string; // Version specified in package.json
duplicates?: string[]; // Other versions found in dependency tree
};
}
// Package information structure for global packages
interface GlobalPackageInfo {
[packageName: string]: string; // packageName: version
}Dependency Tree Analysis:
// Full dependency tree with duplicates
const [name, fullTree] = await getnpmPackages(true, {
fullTree: true,
duplicates: true
});
// Example result structure:
const example = {
'lodash': {
installed: '4.17.21', // Top-level version
wanted: '^4.17.0', // Version range from package.json
duplicates: ['4.17.20', '4.17.19'] // Other versions in tree
},
'react': {
installed: '17.0.2',
wanted: '^17.0.0',
duplicates: []
}
};Pattern Matching:
// Webpack-related packages
const [name, webpackPackages] = await getnpmPackages('*webpack*');
// Babel packages (scoped)
const [name, babelPackages] = await getnpmPackages('@babel/*');
// Complex patterns
const [name, complexPattern] = await getnpmPackages('{*babel*,*webpack*,eslint}');
// Testing frameworks
const [name, testPackages] = await getnpmPackages('{jest,mocha,chai,*test*}');Package management integrates seamlessly with core envinfo functions:
const envinfo = require('envinfo');
// Include packages in main environment report
const result = await envinfo.run({
System: ['OS', 'CPU'],
Binaries: ['Node', 'npm'],
npmPackages: ['react', 'vue', 'angular'], // Specific packages
npmGlobalPackages: true // All global packages
}, {
duplicates: true,
showNotFound: true,
json: true
});
// Using presets that include package analysis
const reactNativeResult = await envinfo.run('react-native');
// Automatically includes React Native related packages
const jestResult = await envinfo.run('jest');
// Automatically includes Jest and related testing packages// Pattern matching capabilities
const patterns = {
// All packages starting with 'babel'
babel: '*babel*',
// All scoped Angular packages
angular: '@angular/*',
// Multiple specific patterns
testing: '{jest,mocha,chai,*test*}',
// Build tools
build: '{webpack,rollup,parcel,*build*}',
// React ecosystem
react: '{react,react-*,@react/*}',
// Development tools
dev: '{eslint,prettier,*lint*,*format*}'
};
// Use any pattern
const [name, matches] = await getnpmPackages(patterns.react);// Handling missing packages
const [name, packages] = await getnpmPackages(['missing-package'], {
showNotFound: true
});
console.log(packages);
// Result: { 'missing-package': 'Not Found' }
// Handling empty node_modules
const [name, emptyResult] = await getnpmPackages(true);
console.log(emptyResult);
// Result: {} (empty object)
// Handling malformed package.json files
// Gracefully skips packages with invalid package.json
const [name, validPackages] = await getnpmPackages(true, { fullTree: true });// For better performance with large projects:
// Get only top-level packages (fastest)
const [name, topLevel] = await getnpmPackages(true);
// Get specific packages only (faster than full tree)
const [name, specific] = await getnpmPackages(['react', 'lodash']);
// Full tree analysis (slowest but most complete)
const [name, fullTree] = await getnpmPackages(true, {
fullTree: true,
duplicates: true
});