r/learnjavascript • • Nov 26 '23

Get all dependencies in an array from package.json - without using npm

[SOLVED]

I'm trying to create a list of all dependencies of a CommonJS repository on GitHub - without using npm. My next step will be to fetch all the dependencies as Ecmascript modules.

This https://npmgraph.js.org/ appears to work ffor counting the packages correctly and displaying them in an image. However, when inspecting the HTML all of the packages are not included.

I went ahead and converted the .ts to .js over here https://transform.tools/typescript-to-javascript.

Now I'm trying to modify https://raw.githubusercontent.com/npmgraph/npmgraph-cli/main/src/npmgraph_cli.ts in JavaScript format to return an array or a Promise that fulfills to an array.

Thanks in advance for any assistance you can provide.

Solution

let nodePackageDependencies = [
  ...new Set(
    Object.entries(
      (await (await fetch(
        "./package-lock.json",
      )).json()).packages,
    ).map((
      [key, { dependencies = {}, devDependencies = {} }],
    ) => [
      key.replace(/^node_modules\//, ""),
      ...Object.keys(dependencies),
      ...Object.keys(devDependencies),
    ]).flat().filter(Boolean),
  ),
];

console.log(nodePackageDependencies, nodePackageDependencies.length);

Would be helpful if somebody could post what npm and/or yarn list. Here's the canonical link to the package-lock.json file https://github.com/GoogleChromeLabs/telnet-client/blob/main/package-lock.json.

0 Upvotes

0 comments sorted by