
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
npm-logical-tree
Advanced tools
The npm-logical-tree package is a utility for generating and manipulating logical trees of npm dependencies. It helps in understanding the structure and relationships of dependencies in a project.
Generate Logical Tree
This feature allows you to generate a logical tree from a package.json file. The logical tree represents the hierarchical structure of dependencies in a project.
const logicalTree = require('npm-logical-tree');
const packageJson = require('./package.json');
const tree = logicalTree(packageJson);
console.log(tree);
Traverse Logical Tree
This feature allows you to traverse the logical tree and perform operations on each node. In this example, it prints the name of each dependency in the tree.
const logicalTree = require('npm-logical-tree');
const packageJson = require('./package.json');
const tree = logicalTree(packageJson);
function traverseTree(node) {
console.log(node.name);
if (node.dependencies) {
Object.values(node.dependencies).forEach(traverseTree);
}
}
traverseTree(tree);
Filter Dependencies
This feature allows you to filter dependencies in the logical tree based on a predicate function. In this example, it prints the names of dependencies that start with 'express'.
const logicalTree = require('npm-logical-tree');
const packageJson = require('./package.json');
const tree = logicalTree(packageJson);
function filterDependencies(node, predicate) {
if (predicate(node)) {
console.log(node.name);
}
if (node.dependencies) {
Object.values(node.dependencies).forEach(child => filterDependencies(child, predicate));
}
}
filterDependencies(tree, node => node.name.startsWith('express'));
The npm-dependency-tree package generates a dependency tree for a given npm module. It provides similar functionality to npm-logical-tree but focuses more on visualizing the dependency graph.
Madge is a tool that creates visualizations of module dependencies. It can generate dependency graphs and identify circular dependencies. While it offers more visualization options, it may not provide the same level of detail in logical tree structures as npm-logical-tree.
Depcheck is a tool that helps you find unused dependencies in your project. While it doesn't generate a logical tree, it provides insights into which dependencies are actually being used, complementing the functionality of npm-logical-tree.
npm-logical-tree
is a Node.js
library that takes the contents of a package.json
and package-lock.json
(or
npm-shrinkwrap.json
) and returns a nested tree data structure representing the
logical relationships between the different dependencies.
$ npm install npm-logical-tree
const fs = require('fs')
const logicalTree = require('npm-logical-tree')
const pkg = require('./package.json')
const pkgLock = require('./package-lock.json')
logicalTree(pkg, pkgLock)
// returns:
LogicalTree {
name: 'npm-logical-tree',
version: '1.0.0',
address: null,
optional: false,
dev: false,
bundled: false,
resolved: undefined,
integrity: undefined,
dependencies:
Map {
'foo' => LogicalTree {
name: 'foo',
version: '1.2.3',
address: 'foo',
optional: false,
dev: true,
bundled: false,
resolved: 'https://registry.npmjs.org/foo/-/foo-1.2.3.tgz',
integrity: 'sha1-rYUK/p261/SXByi0suR/7Rw4chw=',
dependencies: Map { ... }
pending: null
},
...
}
}
The npm team enthusiastically welcomes contributions and project participation! There's a bunch of things you can do if you want to contribute! The Contributor Guide has all the information you need for everything from reporting bugs to contributing entire new features. Please don't hesitate to jump in if you'd like to, or even ask us questions if something isn't clear.
All participants and maintainers in this project are expected to follow Code of Conduct, and just generally be excellent to each other.
Please refer to the Changelog for project history details, too.
Happy hacking!
> logicalTree(pkg, lock) -> LogicalTree
Calculates a logical tree based on a matching package.json
and
package-lock.json
pair. A "logical tree" is a fully-nested dependency graph
for an npm package, as opposed to a physical tree which might be flattened.
logical-tree
will represent deduplicated/flattened nodes using the same object
throughout the tree, so duplication can be checked by object identity.
const pkg = require('./package.json')
const pkgLock = require('./package-lock.json')
logicalTree(pkg, pkgLock)
// returns:
LogicalTree {
name: 'npm-logical-tree',
version: '1.0.0',
address: null,
optional: false,
dev: false,
bundled: false,
resolved: undefined,
integrity: undefined,
dependencies:
Map {
'foo' => LogicalTree {
name: 'foo',
version: '1.2.3',
address: 'foo',
optional: false,
dev: true,
bundled: false,
resolved: 'https://registry.npmjs.org/foo/-/foo-1.2.3.tgz',
integrity: 'sha1-rYUK/p261/SXByi0suR/7Rw4chw=',
dependencies: Map { ... }
pending: null
},
...
}
}
FAQs
Calculate 'logical' trees from a package.json + package-lock
The npm package npm-logical-tree receives a total of 280,412 weekly downloads. As such, npm-logical-tree popularity was classified as popular.
We found that npm-logical-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.