Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I identify which npm packages are just peer dependencies?

I'm trying to remove unused packages from the package.json files for a few projects but I'm running into issues with peer dependencies. There are some tools, such as depcheck, which try to list all of the "unused" packages, but it doesn't differentiate between actual unused packages, and packages that are unused because they're peer dependencies.

Is there a package out there, or some npm command I'm not familiar with, that will allow me to either list all peer dependencies in my project or, at the very least, allow me to type in a package name and see if that package is installed because it's a peer dependency of another package?

For posterity, here's an example of just the dependencies for one of my projects. In this project, I know for instance that reflect-metadata is a peer dependency of @nestjs/common, but I only discovered that after uninstalling it.

"dependencies": {
    "@google-cloud/storage": "^3.2.1",
    "@google-cloud/vision": "^1.3.0",
    "@google/maps": "^0.5.5",
    "@nestjs/common": "^6.6.7",
    "@nestjs/core": "^6.6.7",
    "@nestjs/platform-express": "^6.6.7",
    "@slack/webhook": "^5.0.1",
    "@typeform/api-client": "^1.5.1",
    "algoliasearch": "^3.34.0",
    "array-uniq": "^2.1.0",
    "basic-auth": "^2.0.1",
    "child-process-promise": "^2.2.1",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.10.0",
    "express": "^4.17.1",
    "firebase-admin": "^8.5.0",
    "firebase-functions": "^3.2.0",
    "geoip-lite": "^1.3.8",
    "geolib": "^3.0.4",
    "glob": "^7.1.4",
    "hbs": "^4.0.4",
    "hubspot-api": "^2.2.10",
    "json2csv": "^4.5.3",
    "lodash": "^4.17.15",
    "luxon": "^1.17.2",
    "node-fetch": "^2.6.0",
    "postmark": "^2.2.9",
    "promise-settle": "^0.3.0",
    "qrcode": "^1.4.1",
    "redux": "^4.0.4",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^6.5.3",
    "sales-tax": "^2.0.10",
    "sanitize-filename": "^1.6.3",
    "sharp": "^0.23.0",
    "stripe": "^7.9.0"
  },
like image 631
Alex Plumb Avatar asked Sep 24 '19 14:09

Alex Plumb


People also ask

What are npm peer dependencies?

Peer Dependencies are used to specify that our package is compatible with a specific version of an npm package. Good examples are Angular and React. To add a Peer Dependency you actually need to manually modify your package.json file.

How do I see npm dependencies?

Use the npm list to show the installed packages in the current project as a dependency tree. Use npm list --depth=n to show the dependency tree with a specified depth. Use npm list --prod to show packages in the dependencies . Use npm list --dev to show packages in the devDependencies .

Does npm automatically install peer dependencies?

With npm version 4 through to 6, a warning is issued when you run npm install to remind you to install the peer dependencies. Prior to version 4, npm automatically included peer dependencies if they weren't explicitly included.


1 Answers

This is a great question, not sure why it was downvoted. Unfortunately I don't know of an existing, nicely automated way to do this.

You can test an individual package like so:

npm uninstall some-package && npm ls

If there are any peer dependency violations, they will be printed out and the command will exit nonzero.

So you could combine this with the output of one of the other tools mentioned, iterate through the candidates for orphaned packages, remove them one-by-one, and test the output between each change. Then do an npm uninstall --save to commit the ones that didn't produce an error, or npm install to roll back the ones that do. This could be automated, but I will leave that as an exercise to the reader.

like image 159
Eliot Avatar answered Nov 14 '22 21:11

Eliot