Package Exports
- get-modules-graph
Readme
get-modules-graph
Get and traverse graph of ECMAScript/TypeScript modules.
Basic example
import {dirname, join, sep} from 'node:path';
import {getModulesGraph} from 'get-modules-graph';
const modulesGraph = await getModulesGraph<number>({
chooseIndexModule: (resolvedPath, directoryPath, directoryContent) => {
if ('index.ts' in directoryContent) {
return 'index.ts';
}
throw new Error(
`Cannot choose index module in directory \`${directoryPath}\` for \`${resolvedPath}\``,
);
},
chooseModule: (resolvedPath, parsedPath, directoryContent) => {
const fileName = `${parsedPath.base}.ts`;
if (fileName in directoryContent) {
return fileName;
}
throw new Error(`Cannot choose module for \`${resolvedPath}\``);
},
directories: [],
modules: ['./index.ts'],
onAddDependencies: () => {},
onAddModule: (_module, source) => source.length,
resolvePath: (modulePath, rawPath) => {
if (rawPath[0] === '.') {
return `.${sep}${join(dirname(modulePath), rawPath)}`;
}
return rawPath;
},
skipDirectory: () => false,
skipModule: () => false,
transformSource: (_path, source) => source,
});
console.log(modulesGraph.circularDependencies);
console.log(modulesGraph.errors);
console.log(modulesGraph.modules);
console.log(modulesGraph.packages);Install
Requires node version 10 or higher:
npm install get-modules-graphget-modules-graph works in any environment that supports ES2018
(because package uses RegExp Named Capture Groups).