10 examples of 'npm dependency tree' in JavaScript

Every line of 'npm dependency tree' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
68var getDependencyTree = function getDependencyTree() {
69 var result = syncExec('npm ls --json ' + productionModifier);
70 return JSON.parse(result.stdout).dependencies;
71};
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

105async function npmDeps(location) {
106 let pkgInfo = await fs.readFile(path.resolve(location, 'package.json'));
107 pkgInfo = parseJson(pkgInfo);
108 return pkgInfo.dependencies;
109}
13function asYarnDependency(prefix, tree) {
14 let parseResult;
15
16 try {
17 parseResult = parseSemver(tree.name);
18 } catch (err) {
19 err.message += `: ${tree.name}`;
20 console.warn(`Could not parse semver: ${tree.name}`);
21 return null;
22 }
23
24 // not an actual dependency in disk
25 if (parseResult.version !== parseResult.range) {
26 return null;
27 }
28
29 const name = parseResult.name;
30 const version = parseResult.version;
31 const dependencyPath = path.join(prefix, name);
32 const children = [];
33
34 for (const child of (tree.children || [])) {
35 const dep = asYarnDependency(path.join(prefix, name, 'node_modules'), child);
36
37 if (dep) {
38 children.push(dep);
39 }
40 }
41
42 return { name, version, path: dependencyPath, children };
43}
3export function addToDepTree (file, dep) {
4 depTree[file] = depTree[file] || []
5 depTree[file].push(dep)
6}
38function isDep (tree, child, cb) {
39 var name = moduleName(child)
40 var prodVer = isProdDep(tree, name)
41 var devVer = isDevDep(tree, name)
42
43 childDependencySpecifier(tree, name, prodVer, function (er, prodSpec) {
44 if (er) return cb(child.fromShrinkwrap)
45 var matches
46 if (prodSpec) matches = doesChildVersionMatch(child, prodSpec, tree)
47 if (matches) return cb(true, prodSpec)
48 if (devVer === prodVer) return cb(child.fromShrinkwrap)
49 childDependencySpecifier(tree, name, devVer, function (er, devSpec) {
50 if (er) return cb(child.fromShrinkwrap)
51 cb(doesChildVersionMatch(child, devSpec, tree) || child.fromShrinkwrap, null, devSpec)
52 })
53 })
54}
20function getModulesAndDependencies(tree) {
21 var moduleList = [];
22 var ignoreList = [];
23
24 for (var module in tree) {
25 var dependencies = tree[module];
26
27 moduleList.push(slash(module));
28
29 var re = /goo\/[^\/]+pack\//
30 dependencies.forEach(function(dependency) {
31 if (!re.test(dependency)) {
32 if (ignoreList.indexOf(dependency) === -1) {
33 ignoreList.push(dependency);
34 }
35 }
36 });
37 }
38
39 return {
40 moduleList: moduleList,
41 ignoreList: ignoreList
42 };
43}
75function asYarnDependency(prefix: string, tree: YarnTreeNode, prune: boolean): YarnDependency | null {
76 if (prune && /@[\^~]/.test(tree.name)) {
77 return null;
78 }
79
80 let name: string, version: string;
81
82 try {
83 const parseResult = parseSemver(tree.name);
84 name = parseResult.name;
85 version = parseResult.version;
86 } catch (err) {
87 console.error('Failed to parse dependency:', tree.name);
88 return null;
89 }
90
91 const dependencyPath = path.join(prefix, name);
92 const children: YarnDependency[] = [];
93
94 for (const child of (tree.children || [])) {
95 const dep = asYarnDependency(path.join(prefix, name, 'node_modules'), child, prune);
96
97 if (dep) {
98 children.push(dep);
99 }
100 }
101
102 return { name, version, path: dependencyPath, children };
103}
67export function addDependencyToPackageJson(tree: Tree, packageName: string, packageVersion: string, force = false) {
68 if (!tree.exists(packageJsonName)) {
69 throwNoPackageJsonError();
70 }
71
72 const packageJson: PackageJson = readJSON(tree, packageJsonName);
73
74 if (!packageJson.dependencies) {
75 packageJson.dependencies = {};
76 }
77
78 if (!packageJson.dependencies[packageName] || force) {
79 packageJson.dependencies[packageName] = packageVersion;
80 packageJson.dependencies = sortObjectByKeys(packageJson.dependencies);
81 }
82
83 writeJSON(tree, packageJsonName, packageJson);
84}
151readNPMDependencies() {
152 return this.readDependencies(this.project.dependencies(), 'npm');
153}
123function addChild (dep, tree, allDeps, pkgLock) {
124 tree.addDep(dep)
125 allDeps.set(dep.address, dep)
126 const addr = dep.address
127 const lockNode = atAddr(pkgLock, addr)
128 Object.keys(lockNode.requires || {}).forEach(name => {
129 const tdepAddr = reqAddr(pkgLock, name, addr)
130 let tdep = allDeps.get(tdepAddr)
131 if (!tdep) {
132 tdep = makeNode(name, tdepAddr, atAddr(pkgLock, tdepAddr))
133 addChild(tdep, dep, allDeps, pkgLock)
134 } else {
135 dep.addDep(tdep)
136 }
137 })
138}

Related snippets