MCPcopy
hub / github.com/webpack/webpack / isInstalled

Function isInstalled

bin/webpack.js:37–75  ·  view source on GitHub ↗
(packageName)

Source from the content-addressed store, hash-verified

35 * @returns {boolean} is the package installed?
36 */
37const isInstalled = (packageName) => {
38 if (process.versions.pnp) {
39 return true;
40 }
41
42 const path = require("path");
43 const fs = require("graceful-fs");
44
45 let dir = __dirname;
46
47 do {
48 try {
49 if (
50 fs.statSync(path.join(dir, "node_modules", packageName)).isDirectory()
51 ) {
52 return true;
53 }
54 } catch (_error) {
55 // Nothing
56 }
57 } while (dir !== (dir = path.dirname(dir)));
58
59 // https://github.com/nodejs/node/blob/v18.9.1/lib/internal/modules/cjs/loader.js#L1274
60 const { globalPaths } =
61 /** @type {typeof import("module") & { globalPaths: string[] }} */
62 (require("module"));
63
64 for (const internalPath of globalPaths) {
65 try {
66 if (fs.statSync(path.join(internalPath, packageName)).isDirectory()) {
67 return true;
68 }
69 } catch (_error) {
70 // Nothing
71 }
72 }
73
74 return false;
75};
76
77/**
78 * @param {CliOption} cli options

Callers 1

webpack.jsFile · 0.85

Calls 1

requireFunction · 0.50

Tested by

no test coverage detected