How to list all required npm peerDependencies
Asked Answered
T

3

10

How can I list out all currently required peer dependencies in my project?

I've tried searching, but I only find a bunch of custom packages to check peer dependencies. I already know they're currently ok, but I want a list of what packages and versions they actually are.

Any ideas on how to do this?

Tewfik answered 12/8, 2019 at 7:57 Comment(0)
H
12

If searching by the name of a package then running the command below gives peer depdencies

npm info bootstrap peerDependencies
{ jquery: '1.9.1 - 3', 'popper.js': '^1.16.0' }

Running it on the entire package.json file is something I am not familiar with

Humeral answered 20/3, 2020 at 9:38 Comment(1)
Glad to share this command. thanksMiserere
G
3

You can do it with the following bash script started from your project folder. For ease of understandability it calls node for each matching package.json. To improve performance you can replace the multiple here documents by a single one (arround the for loop) and call node once.

#!/bin/bash
for d in $(find node_modules -name package.json \
           -exec grep -lw peerDependencies {} \;)
do
    node << EOF
        const {peerDependencies } = require('./$d');
        for (k in peerDependencies) {
            console.log('File $d:', k, peerDependencies[k]);
        }
EOF
done

And here the more performant version:

#!/bin/bash                                                                      
for d in $(find node_modules -name package.json \                                
           -exec grep -lw peerDependencies {} \;)                                
do                                                                               
        echo "m = require('./$d');                                               
for (k in m.peerDependencies) {                                                  
    console.log('File $d:', k, m.peerDependencies[k]);                           
}"                                                                               
done |                                                                           
node                                  
Gorlin answered 6/12, 2020 at 12:6 Comment(0)
M
1

Here's a one-liner that depends on the jq utility.

jq ".dependencies,.devDependencies | keys | flatten[]" package.json | xargs -t -I {} npm info {} peerDependencies

This will pull the keys from the dependencies and devDependencies objects of your package.json then pipe those keys to npm info, printing the generated command at each step.

Merridie answered 18/8, 2023 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.