What is module-deps?
The module-deps npm package is a tool for analyzing the dependency graph of Node.js modules. It parses the require() calls in a given entry file and recursively resolves the dependencies, providing a detailed graph of all the modules and their interconnections.
What are module-deps's main functionalities?
Dependency Graph Generation
This feature allows you to generate a JSON file that represents the dependency graph of a given entry file. The code sample demonstrates how to use module-deps to analyze 'entry.js' and output the dependency graph to 'deps.json'.
const mdeps = require('module-deps');
const JSONStream = require('JSONStream');
const fs = require('fs');
const md = mdeps();
md.pipe(JSONStream.stringify()).pipe(fs.createWriteStream('deps.json'));
md.end({ file: 'entry.js' });
Custom Resolvers
This feature allows you to provide custom resolution logic for resolving module paths. The code sample shows how to use a custom resolver function to handle module resolution.
const mdeps = require('module-deps');
const JSONStream = require('JSONStream');
const fs = require('fs');
const md = mdeps({
resolve: (id, parent, cb) => {
// Custom resolution logic
cb(null, id);
}
});
md.pipe(JSONStream.stringify()).pipe(fs.createWriteStream('deps.json'));
md.end({ file: 'entry.js' });
Transform Streams
This feature allows you to apply transform streams to the source files before they are parsed. The code sample demonstrates how to replace all instances of 'require' with 'customRequire' in the source files.
const mdeps = require('module-deps');
const through = require('through2');
const JSONStream = require('JSONStream');
const fs = require('fs');
const md = mdeps({
transform: (file) => {
return through(function (buf, enc, next) {
this.push(buf.toString('utf8').replace(/require/g, 'customRequire'));
next();
});
}
});
md.pipe(JSONStream.stringify()).pipe(fs.createWriteStream('deps.json'));
md.end({ file: 'entry.js' });
Other packages similar to module-deps
browserify
Browserify is a tool that allows you to bundle up all of your JavaScript dependencies for the browser. It also provides a way to analyze the dependency graph of your modules. Compared to module-deps, Browserify offers a more comprehensive solution for bundling and dependency management, including support for various plugins and transforms.
webpack
Webpack is a powerful module bundler for JavaScript applications. It analyzes the dependency graph of your modules and bundles them into a single file or multiple chunks. Webpack provides a rich ecosystem of plugins and loaders, making it more versatile than module-deps for complex build processes.
rollup
Rollup is a module bundler for JavaScript that focuses on ES6 modules. It provides tree-shaking capabilities to remove unused code from the final bundle. While module-deps focuses on dependency graph analysis, Rollup is more geared towards optimizing and bundling ES6 modules.
module-deps
walk the dependency graph to generate json output that can be fed into
browser-pack
example
var mdeps = require('module-deps');
var JSONStream = require('JSONStream');
var stringify = JSONStream.stringify();
stringify.pipe(process.stdout);
var file = __dirname + '/files/main.js';
mdeps(file).pipe(stringify);
output:
$ node example/deps.js
[
{"id":"/home/substack/projects/module-deps/example/files/main.js","source":"var foo = require('./foo');\nconsole.log('main: ' + foo(5));\n","entry":true,"deps":{"./foo":"/home/substack/projects/module-deps/example/files/foo.js"}}
,
{"id":"/home/substack/projects/module-deps/example/files/foo.js","source":"var bar = require('./bar');\n\nmodule.exports = function (n) {\n return n * 111 + bar(n);\n};\n","deps":{"./bar":"/home/substack/projects/module-deps/example/files/bar.js"}}
,
{"id":"/home/substack/projects/module-deps/example/files/bar.js","source":"module.exports = function (n) {\n return n * 100;\n};\n","deps":{}}
]
and you can feed this json data into
browser-pack:
$ node example/deps.js | browser-pack | node
main: 1055
usage
usage: module-deps [files]
generate json output from each entry file
methods
var mdeps = require('module-deps')
mdeps(files, opts={})
Return a readable stream of javascript objects from an array of filenames
files
.
Optionally pass in opts
that will be fed through into the underlying
required module.
install
With npm, to get the module do:
npm install module-deps
and to get the module-deps
command do:
npm install -g module-deps
license
MIT