$ npm install resolve-tree
Recursively resolve node.js modules and its dependencies looking in node_modules
trees.
The module resolution algorithm behavies like require.resolve
in node.js.
It also mimics the recursive module resolution behavior used by npm
.
require.resolve
npm install resolve-tree
const resolve = require('resolve-tree')
// Names of the packages to resolve
const names = ['foo', 'bar']
// Custom optional params for the resolution
const opts = {
basedir: process.cwd(),
lookups: ['dependencies', 'devDependencies']
}
resolve.packages(names, opts, function (err, tree) {
if (err) return console.error(err)
const json = JSON.stringify(tree, null, 2)
console.log(json)
})
The resolved dependency tree serialized to JSON looks like this:
[{
"name": "foo",
"manifest": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo/package.json",
"basedir": "/Users/h2non/Projects/resolve-tree/fixtures/simple",
"main": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo/index.js",
"root": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo",
"version": "0.1.0",
"meta": {
"name": "foo",
"dependencies": {
"baz": "~0.1.0",
"bar": "~0.1.0",
"quz": "~0.1.0"
}
},
"dependencies": [{
"name": "baz",
"manifest": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/baz/package.json",
"basedir": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo",
"main": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/baz/index.js",
"root": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/baz",
"version": "0.1.0",
"meta": {
"name": "baz"
}
}, {
"name": "bar",
"manifest": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/bar/package.json",
"basedir": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo",
"main": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/bar/index.js",
"root": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/bar",
"version": "0.1.0",
"meta": {
"name": "bar",
"dependencies": {
"baz": "~0.1.0"
}
},
"dependencies": [{
"name": "baz",
"manifest": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/baz/package.json",
"basedir": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/bar",
"main": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/baz/index.js",
"root": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/baz",
"version": "0.1.0",
"meta": {
"name": "baz"
}
}]
}, {
"name": "quz",
"manifest": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo/node_modules/quz/package.json",
"basedir": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo",
"main": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo/node_modules/quz/index.js",
"root": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo/node_modules/quz",
"version": "0.1.0",
"meta": {
"name": "quz",
"dependencies": {
"baz": "~0.0.1"
}
},
"dependencies": [{
"name": "baz",
"manifest": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo/node_modules/quz/node_modules/baz/package.json",
"basedir": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo/node_modules/quz",
"main": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo/node_modules/quz/node_modules/baz/index.js",
"root": "/Users/h2non/Projects/resolve-tree/fixtures/simple/node_modules/foo/node_modules/quz/node_modules/baz",
"version": "0.1.0",
"meta": {
"name": "baz",
"version": "0.0.1"
}
}]
}]
}]
string
- Base directory path to start lookups. Default to process.cwd()
.array<string>
- Dependency types to lookup. Allowed values are: dependencies
, devDependencies
, peerDependencies
. Defaults to: dependencies
Alias: byName()
Find and resolve modules and its dependencies recursively looking by package name.
Resolve dependencies recursively reading the package.json
metadata.
array<pkg>
Flatten dependency tree to one level structure tree.
array<mixed>
Flatten the given dependency tree mapping by dependency value field or custom mapper function.
object
Map of packages resolution mappings.
{
'mz': 'mz/fs'
}
Alias: byNameSync
Synchronous version of packages
.
Find and resolve modules and its dependencies recursively looking by package name.
Synchronous version of manifest
.
Resolve dependencies recursively reading the package.json
metadata.
MIT - Tomas Aparicio
© 2010 - cnpmjs.org x YWFE | Home | YWFE