• Home
Name Date Size #Lines LOC

..--

bin/12-May-2024-649548

lib/12-May-2024-13,1649,290

LICENSE.mdD12-May-2024798 2115

README.mdD12-May-202417.4 KiB350296

package.jsonD12-May-20242.7 KiB9897

README.md

1# @npmcli/arborist
2
3[![npm version](https://img.shields.io/npm/v/@npmcli/arborist.svg)](https://npm.im/@npmcli/arborist)
4[![license](https://img.shields.io/npm/l/@npmcli/arborist.svg)](https://npm.im/@npmcli/arborist)
5[![CI - @npmcli/arborist](https://github.com/npm/cli/actions/workflows/ci-npmcli-arborist.yml/badge.svg)](https://github.com/npm/cli/actions/workflows/ci-npmcli-arborist.yml)
6
7Inspect and manage `node_modules` trees.
8
9![a tree with the word ARBORIST superimposed on it](https://raw.githubusercontent.com/npm/arborist/main/docs/logo.svg?sanitize=true)
10
11There's more documentation [in the docs
12folder](https://github.com/npm/cli/tree/latest/workspaces/arborist/docs).
13
14## USAGE
15
16```js
17const Arborist = require('@npmcli/arborist')
18
19const arb = new Arborist({
20  // options object
21
22  // where we're doing stuff.  defaults to cwd.
23  path: '/path/to/package/root',
24
25  // url to the default registry.  defaults to npm's default registry
26  registry: 'https://registry.npmjs.org',
27
28  // scopes can be mapped to a different registry
29  '@foo:registry': 'https://registry.foo.com/',
30
31  // Auth can be provided in a couple of different ways.  If none are
32  // provided, then requests are anonymous, and private packages will 404.
33  // Arborist doesn't do anything with these, it just passes them down
34  // the chain to pacote and npm-registry-fetch.
35
36  // Safest: a bearer token provided by a registry:
37  // 1. an npm auth token, used with the default registry
38  token: 'deadbeefcafebad',
39  // 2. an alias for the same thing:
40  _authToken: 'deadbeefcafebad',
41
42  // insecure options:
43  // 3. basic auth, username:password, base64 encoded
44  auth: 'aXNhYWNzOm5vdCBteSByZWFsIHBhc3N3b3Jk',
45  // 4. username and base64 encoded password
46  username: 'isaacs',
47  password: 'bm90IG15IHJlYWwgcGFzc3dvcmQ=',
48
49  // auth configs can also be scoped to a given registry with this
50  // rather unusual pattern:
51  '//registry.foo.com:token': 'blahblahblah',
52  '//basic.auth.only.foo.com:_auth': 'aXNhYWNzOm5vdCBteSByZWFsIHBhc3N3b3Jk',
53  '//registry.foo.com:always-auth': true,
54})
55
56// READING
57
58// returns a promise.  reads the actual contents of node_modules
59arb.loadActual().then(tree => {
60  // tree is also stored at arb.virtualTree
61})
62
63// read just what the package-lock.json/npm-shrinkwrap says
64// This *also* loads the yarn.lock file, but that's only relevant
65// when building the ideal tree.
66arb.loadVirtual().then(tree => {
67  // tree is also stored at arb.virtualTree
68  // now arb.virtualTree is loaded
69  // this fails if there's no package-lock.json or package.json in the folder
70  // note that loading this way should only be done if there's no
71  // node_modules folder
72})
73
74// OPTIMIZING AND DESIGNING
75
76// build an ideal tree from the package.json and various lockfiles.
77arb.buildIdealTree(options).then(() => {
78  // next step is to reify that ideal tree onto disk.
79  // options can be:
80  // rm: array of package names to remove at top level
81  // add: Array of package specifiers to add at the top level.  Each of
82  //   these will be resolved with pacote.manifest if the name can't be
83  //   determined from the spec.  (Eg, `github:foo/bar` vs `foo@somespec`.)
84  //   The dep will be saved in the location where it already exists,
85  //   (or pkg.dependencies) unless a different saveType is specified.
86  // saveType: Save added packages in a specific dependency set.
87  //   - null (default) Wherever they exist already, or 'dependencies'
88  //   - prod: definitely in 'dependencies'
89  //   - optional: in 'optionalDependencies'
90  //   - dev: devDependencies
91  //   - peer: save in peerDependencies, and remove any optional flag from
92  //     peerDependenciesMeta if one exists
93  //   - peerOptional: save in peerDependencies, and add a
94  //     peerDepsMeta[name].optional flag
95  // saveBundle: add newly added deps to the bundleDependencies list
96  // update: Either `true` to just go ahead and update everything, or an
97  //   object with any or all of the following fields:
98  //   - all: boolean.  set to true to just update everything
99  //   - names: names of packages update (like `npm update foo`)
100  // prune: boolean, default true.  Prune extraneous nodes from the tree.
101  // preferDedupe: prefer to deduplicate packages if possible, rather than
102  //   choosing a newer version of a dependency.  Defaults to false, ie,
103  //   always try to get the latest and greatest deps.
104  // legacyBundling: Nest every dep under the node requiring it, npm v2 style.
105  //   No unnecessary deduplication.  Default false.
106
107  // At the end of this process, arb.idealTree is set.
108})
109
110// WRITING
111
112// Make the idealTree be the thing that's on disk
113arb.reify({
114  // write the lockfile(s) back to disk, and package.json with any updates
115  // defaults to 'true'
116  save: true,
117}).then(() => {
118  // node modules has been written to match the idealTree
119})
120```
121
122## DATA STRUCTURES
123
124A `node_modules` tree is a logical graph of dependencies overlaid on a
125physical tree of folders.
126
127A `Node` represents a package folder on disk, either at the root of the
128package, or within a `node_modules` folder.  The physical structure of the
129folder tree is represented by the `node.parent` reference to the containing
130folder, and `node.children` map of nodes within its `node_modules`
131folder, where the key in the map is the name of the folder in
132`node_modules`, and the value is the child node.
133
134A node without a parent is a top of tree.
135
136A `Link` represents a symbolic link to a package on disk.  This can be a
137symbolic link to a package folder within the current tree, or elsewhere on
138disk.  The `link.target` is a reference to the actual node.  Links differ
139from Nodes in that dependencies are resolved from the _target_ location,
140rather than from the link location.
141
142An `Edge` represents a dependency relationship.  Each node has an `edgesIn`
143set, and an `edgesOut` map.  Each edge has a `type` which specifies what
144kind of dependency it represents: `'prod'` for regular dependencies,
145`'peer'` for peerDependencies, `'dev'` for devDependencies, and
146`'optional'` for optionalDependencies.  `edge.from` is a reference to the
147node that has the dependency, and `edge.to` is a reference to the node that
148requires the dependency.
149
150As nodes are moved around in the tree, the graph edges are automatically
151updated to point at the new module resolution targets.  In other words,
152`edge.from`, `edge.name`, and `edge.spec` are immutable; `edge.to` is
153updated automatically when a node's parent changes.
154
155### class Node
156
157All arborist trees are `Node` objects.  A `Node` refers
158to a package folder, which may have children in `node_modules`.
159
160* `node.name` The name of this node's folder in `node_modules`.
161* `node.parent` Physical parent node in the tree.  The package in whose
162  `node_modules` folder this package lives.  Null if node is top of tree.
163
164    Setting `node.parent` will automatically update `node.location` and all
165    graph edges affected by the move.
166
167* `node.meta` A `Shrinkwrap` object which looks up `resolved` and
168  `integrity` values for all modules in this tree.  Only relevant on `root`
169  nodes.
170
171* `node.children` Map of packages located in the node's `node_modules`
172  folder.
173* `node.package` The contents of this node's `package.json` file.
174* `node.path` File path to this package.  If the node is a link, then this
175  is the path to the link, not to the link target.  If the node is _not_ a
176  link, then this matches `node.realpath`.
177* `node.realpath` The full real filepath on disk where this node lives.
178* `node.location` A slash-normalized relative path from the root node to
179  this node's path.
180* `node.isLink` Whether this represents a symlink.  Always `false` for Node
181  objects, always `true` for Link objects.
182* `node.isRoot` True if this node is a root node.  (Ie, if `node.root ===
183  node`.)
184* `node.root` The root node where we are working.  If not assigned to some
185  other value, resolves to the node itself.  (Ie, the root node's `root`
186  property refers to itself.)
187* `node.isTop` True if this node is the top of its tree (ie, has no
188  `parent`, false otherwise).
189* `node.top` The top node in this node's tree.  This will be equal to
190  `node.root` for simple trees, but link targets will frequently be outside
191  of (or nested somewhere within) a `node_modules` hierarchy, and so will
192  have a different `top`.
193* `node.dev`, `node.optional`, `node.devOptional`, `node.peer`, Indicators
194  as to whether this node is a dev, optional, and/or peer dependency.
195  These flags are relevant when pruning dependencies out of the tree or
196  deciding what to reify.  See **Package Dependency Flags** below for
197  explanations.
198* `node.edgesOut` Edges in the dependency graph indicating nodes that this
199  node depends on, which resolve its dependencies.
200* `node.edgesIn` Edges in the dependency graph indicating nodes that depend
201  on this node.
202
203* `extraneous` True if this package is not required by any other for any
204  reason.  False for top of tree.
205
206* `node.resolve(name)`  Identify the node that will be returned when code
207  in this package runs `require(name)`
208
209* `node.errors` Array of errors encountered while parsing package.json or
210  version specifiers.
211
212### class Link
213
214Link objects represent a symbolic link within the `node_modules` folder.
215They have most of the same properties and methods as `Node` objects, with a
216few differences.
217
218* `link.target` A Node object representing the package that the link
219  references.  If this is a Node already present within the tree, then it
220  will be the same object.  If it's outside of the tree, then it will be
221  treated as the top of its own tree.
222* `link.isLink` Always true.
223* `link.children` This is always an empty map, since links don't have their
224  own children directly.
225
226### class Edge
227
228Edge objects represent a dependency relationship a package node to the
229point in the tree where the dependency will be loaded.  As nodes are moved
230within the tree, Edges automatically update to point to the appropriate
231location.
232
233* `new Edge({ from, type, name, spec })`  Creates a new edge with the
234  specified fields.  After instantiation, none of the fields can be
235  changed directly.
236* `edge.from` The node that has the dependency.
237* `edge.type` The type of dependency.  One of `'prod'`, `'dev'`, `'peer'`,
238  or `'optional'`.
239* `edge.name` The name of the dependency.  Ie, the key in the
240  relevant `package.json` dependencies object.
241* `edge.spec` The specifier that is required.  This can be a version,
242  range, tag name, git url, or tarball URL.  Any specifier allowed by npm
243  is supported.
244* `edge.to` Automatically set to the node in the tree that matches the
245  `name` field.
246* `edge.valid` True if `edge.to` satisfies the specifier.
247* `edge.error` A string indicating the type of error if there is a problem,
248  or `null` if it's valid.  Values, in order of precedence:
249    * `DETACHED` Indicates that the edge has been detached from its
250      `edge.from` node, typically because a new edge was created when a
251      dependency specifier was modified.
252    * `MISSING` Indicates that the dependency is unmet.  Note that this is
253      _not_ set for unmet dependencies of the `optional` type.
254    * `PEER LOCAL` Indicates that a `peerDependency` is found in the
255      node's local `node_modules` folder, and the node is not the top of
256      the tree.  This violates the `peerDependency` contract, because it
257      means that the dependency is not a peer.
258    * `INVALID` Indicates that the dependency does not satisfy `edge.spec`.
259* `edge.reload()` Re-resolve to find the appropriate value for `edge.to`.
260  Called automatically from the `Node` class when the tree is mutated.
261
262### Package Dependency Flags
263
264The dependency type of a node can be determined efficiently by looking at
265the `dev`, `optional`, and `devOptional` flags on the node object.  These
266are updated by arborist when necessary whenever the tree is modified in
267such a way that the dependency graph can change, and are relevant when
268pruning nodes from the tree.
269
270```
271| extraneous | peer | dev | optional | devOptional | meaning             | prune?            |
272|------------+------+-----+----------+-------------+---------------------+-------------------|
273|            |      |     |          |             | production dep      | never             |
274|------------+------+-----+----------+-------------+---------------------+-------------------|
275|     X      | N/A  | N/A |   N/A    |     N/A     | nothing depends on  | always            |
276|            |      |     |          |             | this, it is trash   |                   |
277|------------+------+-----+----------+-------------+---------------------+-------------------|
278|            |      |  X  |          |      X      | devDependency, or   | if pruning dev    |
279|            |      |     |          | not in lock | only depended upon  |                   |
280|            |      |     |          |             | by devDependencies  |                   |
281|------------+------+-----+----------+-------------+---------------------+-------------------|
282|            |      |     |    X     |      X      | optionalDependency, | if pruning        |
283|            |      |     |          | not in lock | or only depended on | optional          |
284|            |      |     |          |             | by optionalDeps     |                   |
285|------------+------+-----+----------+-------------+---------------------+-------------------|
286|            |      |  X  |    X     |      X      | Optional dependency | if pruning EITHER |
287|            |      |     |          | not in lock | of dep(s) in the    | dev OR optional   |
288|            |      |     |          |             | dev hierarchy       |                   |
289|------------+------+-----+----------+-------------+---------------------+-------------------|
290|            |      |     |          |      X      | BOTH a non-optional | if pruning BOTH   |
291|            |      |     |          |   in lock   | dep within the dev  | dev AND optional  |
292|            |      |     |          |             | hierarchy, AND a    |                   |
293|            |      |     |          |             | dep within the      |                   |
294|            |      |     |          |             | optional hierarchy  |                   |
295|------------+------+-----+----------+-------------+---------------------+-------------------|
296|            |  X   |     |          |             | peer dependency, or | if pruning peers  |
297|            |      |     |          |             | only depended on by |                   |
298|            |      |     |          |             | peer dependencies   |                   |
299|------------+------+-----+----------+-------------+---------------------+-------------------|
300|            |  X   |  X  |          |      X      | peer dependency of  | if pruning peer   |
301|            |      |     |          | not in lock | dev node hierarchy  | OR dev deps       |
302|------------+------+-----+----------+-------------+---------------------+-------------------|
303|            |  X   |     |    X     |      X      | peer dependency of  | if pruning peer   |
304|            |      |     |          | not in lock | optional nodes, or  | OR optional deps  |
305|            |      |     |          |             | peerOptional dep    |                   |
306|------------+------+-----+----------+-------------+---------------------+-------------------|
307|            |  X   |  X  |    X     |      X      | peer optional deps  | if pruning peer   |
308|            |      |     |          | not in lock | of the dev dep      | OR optional OR    |
309|            |      |     |          |             | hierarchy           | dev               |
310|------------+------+-----+----------+-------------+---------------------+-------------------|
311|            |  X   |     |          |      X      | BOTH a non-optional | if pruning peers  |
312|            |      |     |          |   in lock   | peer dep within the | OR:               |
313|            |      |     |          |             | dev hierarchy, AND  | BOTH optional     |
314|            |      |     |          |             | a peer optional dep | AND dev deps      |
315+------------+------+-----+----------+-------------+---------------------+-------------------+
316```
317
318* If none of these flags are set, then the node is required by the
319  dependency and/or peerDependency hierarchy.  It should not be pruned.
320* If _both_ `node.dev` and `node.optional` are set, then the node is an
321  optional dependency of one of the packages in the devDependency
322  hierarchy.  It should be pruned if _either_ dev or optional deps are
323  being removed.
324* If `node.dev` is set, but `node.optional` is not, then the node is
325  required in the devDependency hierarchy.  It should be pruned if dev
326  dependencies are being removed.
327* If `node.optional` is set, but `node.dev` is not, then the node is
328  required in the optionalDependency hierarchy.  It should be pruned if
329  optional dependencies are being removed.
330* If `node.devOptional` is set, then the node is a (non-optional)
331  dependency within the devDependency hierarchy, _and_ a dependency
332  within the `optionalDependency` hierarchy.  It should be pruned if
333  _both_ dev and optional dependencies are being removed.
334* If `node.peer` is set, then all the same semantics apply as above, except
335  that the dep is brought in by a peer dep at some point, rather than a
336  normal non-peer dependency.
337
338Note: `devOptional` is only set in the shrinkwrap/package-lock file if
339_neither_ `dev` nor `optional` are set, as it would be redundant.
340
341## BIN
342
343Arborist 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.
344
345To see the usage run:
346
347```
348npx @npmcli/arborist --help
349```
350