Manage node_modules trees
npm install @npmcli/arborist


Inspect and manage node_modules trees.
!a tree with the word ARBORIST superimposed on it
There's more documentation in the docs
folder.
``js
const Arborist = require('@npmcli/arborist')
const arb = new Arborist({
// options object
// where we're doing stuff. defaults to cwd.
path: '/path/to/package/root',
// url to the default registry. defaults to npm's default registry
registry: 'https://registry.npmjs.org',
// scopes can be mapped to a different registry
'@foo:registry': 'https://registry.foo.com/',
// Auth can be provided in a couple of different ways. If none are
// provided, then requests are anonymous, and private packages will 404.
// Arborist doesn't do anything with these, it just passes them down
// the chain to pacote and npm-registry-fetch.
// Safest: a bearer token provided by a registry:
// 1. an npm auth token, used with the default registry
token: 'deadbeefcafebad',
// 2. an alias for the same thing:
_authToken: 'deadbeefcafebad',
// insecure options:
// 3. basic auth, username:password, base64 encoded
auth: 'aXNhYWNzOm5vdCBteSByZWFsIHBhc3N3b3Jk',
// 4. username and base64 encoded password
username: 'isaacs',
password: 'bm90IG15IHJlYWwgcGFzc3dvcmQ=',
// auth configs can also be scoped to a given registry with this
// rather unusual pattern:
'//registry.foo.com:token': 'blahblahblah',
'//basic.auth.only.foo.com:_auth': 'aXNhYWNzOm5vdCBteSByZWFsIHBhc3N3b3Jk',
'//registry.foo.com:always-auth': true,
})
// READING
// returns a promise. reads the actual contents of node_modules
arb.loadActual().then(tree => {
// tree is also stored at arb.virtualTree
})
// read just what the package-lock.json/npm-shrinkwrap says
// This also loads the yarn.lock file, but that's only relevant
// when building the ideal tree.
arb.loadVirtual().then(tree => {
// tree is also stored at arb.virtualTree
// now arb.virtualTree is loaded
// this fails if there's no package-lock.json or package.json in the folder
// note that loading this way should only be done if there's no
// node_modules folder
})
// OPTIMIZING AND DESIGNING
// build an ideal tree from the package.json and various lockfiles.
arb.buildIdealTree(options).then(() => {
// next step is to reify that ideal tree onto disk.
// options can be:
// rm: array of package names to remove at top level
// add: Array of package specifiers to add at the top level. Each of
// these will be resolved with pacote.manifest if the name can't be
// determined from the spec. (Eg, github:foo/bar vs foo@somespec.)true
// The dep will be saved in the location where it already exists,
// (or pkg.dependencies) unless a different saveType is specified.
// saveType: Save added packages in a specific dependency set.
// - null (default) Wherever they exist already, or 'dependencies'
// - prod: definitely in 'dependencies'
// - optional: in 'optionalDependencies'
// - dev: devDependencies
// - peer: save in peerDependencies, and remove any optional flag from
// peerDependenciesMeta if one exists
// - peerOptional: save in peerDependencies, and add a
// peerDepsMeta[name].optional flag
// saveBundle: add newly added deps to the bundleDependencies list
// update: Either to just go ahead and update everything, or annpm update foo
// object with any or all of the following fields:
// - all: boolean. set to true to just update everything
// - names: names of packages update (like )
// prune: boolean, default true. Prune extraneous nodes from the tree.
// preferDedupe: prefer to deduplicate packages if possible, rather than
// choosing a newer version of a dependency. Defaults to false, ie,
// always try to get the latest and greatest deps.
// legacyBundling: Nest every dep under the node requiring it, npm v2 style.
// No unnecessary deduplication. Default false.
// At the end of this process, arb.idealTree is set.
})
// WRITING
// Make the idealTree be the thing that's on disk
arb.reify({
// write the lockfile(s) back to disk, and package.json with any updates
// defaults to 'true'
save: true,
}).then(() => {
// node modules has been written to match the idealTree
})
`
A node_modules tree is a logical graph of dependencies overlaid on a
physical tree of folders.
A Node represents a package folder on disk, either at the root of thenode_modules
package, or within a folder. The physical structure of thenode.parent
folder tree is represented by the reference to the containingnode.children
folder, and map of nodes within its node_modulesnode_modules
folder, where the key in the map is the name of the folder in, and the value is the child node.
A node without a parent is a top of tree.
A Link represents a symbolic link to a package on disk. This can be alink.target
symbolic link to a package folder within the current tree, or elsewhere on
disk. The is a reference to the actual node. Links differ
from Nodes in that dependencies are resolved from the _target_ location,
rather than from the link location.
An Edge represents a dependency relationship. Each node has an edgesInedgesOut
set, and an map. Each edge has a type which specifies what'prod'
kind of dependency it represents: for regular dependencies,'peer' for peerDependencies, 'dev' for devDependencies, and'optional' for optionalDependencies. edge.from is a reference to theedge.to
node that has the dependency, and is a reference to the node that
requires the dependency.
As nodes are moved around in the tree, the graph edges are automatically
updated to point at the new module resolution targets. In other words,
edge.from, edge.name, and edge.spec are immutable; edge.to is
updated automatically when a node's parent changes.
All arborist trees are Node objects. A Node refersnode_modules
to a package folder, which may have children in .
* node.name The name of this node's folder in node_modules.node.parent
* Physical parent node in the tree. The package in whosenode_modules
folder this package lives. Null if node is top of tree.
Setting node.parent will automatically update node.location and all
graph edges affected by the move.
* node.meta A Shrinkwrap object which looks up resolved andintegrity
values for all modules in this tree. Only relevant on root
nodes.
* node.children Map of packages located in the node's node_modulesnode.package
folder.
* The contents of this node's package.json file.node.path
* File path to this package. If the node is a link, then thisnode.realpath
is the path to the link, not to the link target. If the node is _not_ a
link, then this matches .node.realpath
* The full real filepath on disk where this node lives.node.location
* A slash-normalized relative path from the root node tonode.isLink
this node's path.
* Whether this represents a symlink. Always false for Nodetrue
objects, always for Link objects.node.isRoot
* True if this node is a root node. (Ie, if node.root ===
node.)node.root
* The root node where we are working. If not assigned to someroot
other value, resolves to the node itself. (Ie, the root node's node.isTop
property refers to itself.)
* True if this node is the top of its tree (ie, has noparent
, false otherwise).node.top
* The top node in this node's tree. This will be equal tonode.root
for simple trees, but link targets will frequently be outsidenode_modules
of (or nested somewhere within) a hierarchy, and so willtop
have a different .node.dev
* , node.optional, node.devOptional, node.peer, Indicatorsnode.edgesOut
as to whether this node is a dev, optional, and/or peer dependency.
These flags are relevant when pruning dependencies out of the tree or
deciding what to reify. See Package Dependency Flags below for
explanations.
* Edges in the dependency graph indicating nodes that thisnode.edgesIn
node depends on, which resolve its dependencies.
* Edges in the dependency graph indicating nodes that depend
on this node.
* extraneous True if this package is not required by any other for any
reason. False for top of tree.
* node.resolve(name) Identify the node that will be returned when coderequire(name)
in this package runs
* node.errors Array of errors encountered while parsing package.json or
version specifiers.
Link objects represent a symbolic link within the node_modules folder.Node
They have most of the same properties and methods as objects, with a
few differences.
* link.target A Node object representing the package that the linklink.isLink
references. If this is a Node already present within the tree, then it
will be the same object. If it's outside of the tree, then it will be
treated as the top of its own tree.
* Always true.link.children
* This is always an empty map, since links don't have their
own children directly.
Edge objects represent a dependency relationship a package node to the
point in the tree where the dependency will be loaded. As nodes are moved
within the tree, Edges automatically update to point to the appropriate
location.
* new Edge({ from, type, name, spec }) Creates a new edge with theedge.from
specified fields. After instantiation, none of the fields can be
changed directly.
* The node that has the dependency.edge.type
* The type of dependency. One of 'prod', 'dev', 'peer','optional'
or .edge.name
* The name of the dependency. Ie, the key in thepackage.json
relevant dependencies object.edge.spec
* The specifier that is required. This can be a version,edge.to
range, tag name, git url, or tarball URL. Any specifier allowed by npm
is supported.
* Automatically set to the node in the tree that matches thename
field.edge.valid
* True if edge.to satisfies the specifier.edge.error
* A string indicating the type of error if there is a problem,null
or if it's valid. Values, in order of precedence:DETACHED
* Indicates that the edge has been detached from itsedge.from
node, typically because a new edge was created when aMISSING
dependency specifier was modified.
* Indicates that the dependency is unmet. Note that this isoptional
_not_ set for unmet dependencies of the type.PEER LOCAL
* Indicates that a peerDependency is found in thenode_modules
node's local folder, and the node is not the top ofpeerDependency
the tree. This violates the contract, because itINVALID
means that the dependency is not a peer.
* Indicates that the dependency does not satisfy edge.spec.edge.reload()
* Re-resolve to find the appropriate value for edge.to.Node
Called automatically from the class when the tree is mutated.
The dependency type of a node can be determined efficiently by looking at
the dev, optional, and devOptional flags on the node object. These
are updated by arborist when necessary whenever the tree is modified in
such a way that the dependency graph can change, and are relevant when
pruning nodes from the tree.
| extraneous | peer | dev | optional | devOptional | meaning | prune? |
|:----------:|:----:|:---:|:--------:|:----------------:|:-------------------------------------------------------------------------------------------------|:-------------------------------------------------------|
| | | | | | production dep | never |
| X | N/A | N/A | N/A | N/A | nothing depends on this, it is trash | always |
| | | X | | X
not in lock | devDependency, or only depended
on by devDependencies | if pruning dev |
| | | | X | X
not in lock | optionalDependency, or only depended
on by optionalDeps | if pruning optional |
| | | X | X | X
not in lock | Optional dependency of dep(s) in the
dev hierarchy | if pruning EITHER
dev OR optional |
| | | | | X
in lock | BOTH a non-optional dep within the
dev hierarchy, AND a dep within
the optional hierarchy | if pruning BOTH
dev AND optional |
| | X | | | | peer dependency, or only depended
on by peer dependencies | if pruning peers |
| | X | X | | X
not in lock | peer dependency of dev node hierarchy | if pruning peer OR
dev deps |
| | X | | X | X
not in lock | peer dependency of optional nodes, or
peerOptional dep | if pruning peer OR
optional deps |
| | X | X | X | X
not in lock | peer optional deps of the dev dep hierarchy | if pruning peer OR
optional OR dev |
| | X | | | X
in lock | BOTH a non-optional peer dep within the
dev hierarchy, AND a peer optional dep | if pruning peer deps OR:
BOTH optional AND dev deps |
* If none of these flags are set, then the node is required by the
dependency and/or peerDependency hierarchy. It should not be pruned.
* If _both_ node.dev and node.optional are set, then the node is annode.dev
optional dependency of one of the packages in the devDependency
hierarchy. It should be pruned if _either_ dev or optional deps are
being removed.
* If is set, but node.optional is not, then the node isnode.optional
required in the devDependency hierarchy. It should be pruned if dev
dependencies are being removed.
* If is set, but node.dev is not, then the node isnode.devOptional
required in the optionalDependency hierarchy. It should be pruned if
optional dependencies are being removed.
* If is set, then the node is a (non-optional)optionalDependency
dependency within the devDependency hierarchy, _and_ a dependency
within the hierarchy. It should be pruned ifnode.peer
_both_ dev and optional dependencies are being removed.
* If is set, then all the same semantics apply as above, except
that the dep is brought in by a peer dep at some point, rather than a
normal non-peer dependency.
Note: devOptional is only set in the shrinkwrap/package-lock file ifdev
_neither_ nor optional are set, as it would be redundant.
Arborist ships with a cli that can be used to run arborist specific commands outside of the context of the npm CLI. This script is currently not part of the public API and is subject to breaking changes outside of major version bumps.
To see the usage run:
```
npx @npmcli/arborist --help